Microsoft 365 and Office | Word | For home | Windows
A family of Microsoft word processing software products for creating web, email, and print documents.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
A family of Microsoft word processing software products for creating web, email, and print documents.
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