View Full Version : [SOLVED:] How to highlight text between two finds
DanMan
01-26-2023, 10:50 AM
Hello,
I have a MS Word document that has a lot of "notes to self" in it. The notes are captured in-between "[" and "]" characters.
How can I create a macro that goes through the entire Word document and highlights all of the text between each pair of [] characters?
thank you.
GCWesq
01-27-2023, 07:21 AM
Here's a start (below). I'm not too good on Word, so I have not been able to figure out how to keep the attached macro running until it reaches the end of the document. You have to keep running it. Perhaps someone else will help out.
Sub Highlighting()
'' Highlighting Macro
Selection.Find.ClearFormatting
With Selection.Find
.Text = "["
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
i = 0
Do While i < 200 'allows 200 characters
i = i + 1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "?"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = "?"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
If (Selection.Text) = "]" Then Exit Sub
Options.DefaultHighlightColorIndex = wdYellow
Selection.Range.HighlightColorIndex = wdYellow
Loop
End Sub
DanMan
01-27-2023, 09:42 AM
Thank you. It's been too long since I've written these things myself.
I think there is a way to find "[" and "]" and select all of the text in between, but this does work for a single instance, so it's a start. TY
GCWesq
01-27-2023, 05:37 PM
Further to my inelegant solution, you can get it to do all by adding the following code at the top, as shown, and change the If statement at the bottom, as shown. It will just keep doing the same thing until j runs out. If you can make j a little more than the number of instances of [], it will work.
Sub Highlighting()
'
' Highlighting Macro
j = 0
continue:
j = j + 1
If j = 5 Then Exit Sub
.
.
.
If (Selection.Text) = "]" Then GoTo continue
DanMan
01-28-2023, 11:12 AM
Thank you. It does work. BTW I added a "^" character at the end of my file and then added this line to the code to make it stop when it reaches the end of the file.
If (Selection.Text) = "^" Then Exit Sub
Thank you!
GCWesq
01-28-2023, 10:20 PM
Great idea! :clap:
hueljannie
12-14-2023, 12:27 AM
Hello,
I have a MS Word document that has a lot of "notes to self" in it. The notes are captured in-between "[" and "]" characters.
How can I create a macro that goes through the entire Word document and highlights all of the text between each pair of [] characters?
thank you.
A beginning is provided below. My Word skills are lacking, thus I haven't figured out how to make the attached macro run all the way to the document's conclusion. It must be kept functioning. Maybe someone else will pitch in as well.
Aussiebear
12-14-2023, 01:56 AM
Maybe try this on a copy of your document
Sub FindSquareBracketPairs()
Dim rngFind As Word.Range
Dim sOpen As String, sClose As String
Dim sFindTerm As String
Dim bFound As Boolean, lPosOpen As Long
Set rngFind = ActiveDocument.content
sOpen = "["
sClose = "]"
sFindTerm = "\[*\]"
With rngFind.Find
.ClearFormatting
.text = "\[*\]"
.Forward = True
.wrap = Word.WdFindWrap.wdFindStop
.MatchWildcards = True
bFound = .Execute
Do While bFound
lPosOpen = NumberOfCharInRange(rngFind, sOpen)
rngFind.HighlightColorIndex = Word.WdColorIndex.wdYellow
rngFind.Collapse wdCollapseEnd
bFound = .Execute
Loop
End With
End Sub
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.