Word move to the second occurrence

SteveD 625 Reputation points
2026-07-18T01:40:22.3533333+00:00

Hello from Steve

objective please is to ignore the first .Text = "|" but move to the second which is directly on the right

Sub Watch()

Selection.Find.ClearFormatting

With Selection.Find

    .Text = "|" 

    .Replacement.Text = " "

    .Forward = True

    .Wrap = wdFindContinue

    .Format = False

    .MatchCase = True

    .MatchWholeWord = False

    .MatchWildcards = False

    .MatchSoundsLike = False

    .MatchAllWordForms = False

End With

Selection.Find.Execute

Selection.MoveRight Unit:=wdCharacter, Count:=1

Selection.TypeText Text:="W"

Selection.Delete

Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

Selection.Font.Color = wdColorRed

Selection.Font.Bold = True

Options.DefaultHighlightColorIndex = wdYellow

Selection.range.HighlightColorIndex = wdYellow

Selection.MoveLeft Unit:=wdCharacter, Count:=3

End Sub

Microsoft 365 and Office | Word | For home | Windows
0 comments No comments

Answer recommended by moderator
SteveD 625 Reputation points
2026-07-18T02:27:49.18+00:00

Hello from Steve

Thanks to copilot which supplied me with the answer.

Sub Watch()

Dim searchText As String

Dim firstPos As Long, secondPos As Long

Dim docText As String

searchText = "|"

docText = Mid(ActiveDocument.Content.Text, Selection.Start + 1)

firstPos = InStr(1, docText, searchText, vbTextCompare)

If firstPos = 0 Then

    MsgBox "First occurrence not found.", vbExclamation

    Exit Sub

End If

secondPos = InStr(firstPos + Len(searchText), docText, searchText, vbTextCompare)

If secondPos = 0 Then

    MsgBox "Second occurrence not found.", vbExclamation

    Exit Sub

End If

Selection.Start = Selection.Start + secondPos - 1

Selection.End = Selection.Start + Len(searchText)

Selection.Select

Selection.MoveRight Unit:=wdCharacter, Count:=1

Selection.TypeText Text:="W"

Selection.Delete

Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

Selection.Font.Color = wdColorRed

Selection.Font.Bold = True

Options.DefaultHighlightColorIndex = wdYellow

Selection.range.HighlightColorIndex = wdYellow

Selection.MoveLeft Unit:=wdCharacter, Count:=3

End Sub

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.