View Full Version : Select all text in brackets
clhare
07-31-2009, 04:27 AM
I need to go through several documents and change any text found in brackets (such as "[this text]") to red.
I know there's probably an easy way to do it, but I can't figure out how.
Thanks!
lucas
07-31-2009, 07:06 AM
From an example that Gerry provided here in the past:
Option Explicit
Sub TryFindKitty()
Call HereKitty("[", "]")
End Sub
Sub HereKitty(sText1 As String, sText2 As String)
Const NoSecondString As String = _
"ummm...sorry but second string was not found."
Dim j As Long
Dim k As Long
Dim r As Range
Dim bolYes As Boolean
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
Do While .Execute(Findtext:=sText1, _
Forward:=True) = True
bolYes = True
j = r.End
r.Expand unit:=wdSentence
With r.Find
If .Execute(Findtext:=sText2, _
Forward:=True) = True Then
k = r.Start
ActiveDocument.Range(Start:=j, _
End:=k).Font.Color = wdColorRed
Else
MsgBox NoSecondString
End If
End With
r.Collapse Direction:=wdCollapseEnd
Loop
End With
If bolYes = False Then
MsgBox "First search phrase not found."
End If
End Sub
fumei
07-31-2009, 08:28 AM
Well, that example is for working with two strings, not one. Since you just want one string - anything between the brackets - you can do it much simpler.
You do not state whether you want to include the brackets, or not. It is always helpful to clearly state what your requirements are. The following includes the brackets.
Sub MakeRed()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.MatchWildcards = True
.ClearFormatting
.Text = "\[*\]"
Do While .Execute(Forward:=True) = True
r.Font.Color = wdColorRed
Loop
End With
End Sub
Demo attached. Click "Make Red" on the top toolbar.
fumei
07-31-2009, 08:35 AM
And if you do NOT want to include the brackets - you just want the text between them to be red - then simply alter the range for each Found.
Move the .Start forward one character (the opening [).
Move the .End backwards on character (the closing ])
Change font colour.
BECAUSE the .Found range has been altered, you must, must, must Collapse the range to its .End. Otherwise you will get an infinite loop. See bolding below.
Sub MakeRed()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.MatchWildcards = True
.ClearFormatting
.Text = "\[*\]"
Do While .Execute(Forward:=True) = True
With r
.MoveStart Unit:=wdCharacter, Count:=1
.MoveEnd Unit:=wdCharacter, Count:=-1
.Font.Color = wdColorRed
.Collapse 0
End With
Loop
End With
End Sub
clhare
07-31-2009, 08:37 AM
I'd like to also change the color of the brackets. How would this need to be changed in order to do that?
clhare
07-31-2009, 08:43 AM
Oops! Didn't refresh my screen so I didn't see the next posts until I posted again.
I do want the brackets to change color as well (sorry I was not clearer). The code works perfectly and will save me alot of time. Also, being able to search for text and exclude the brackets will come in handy!
You guys are awesome!
Paul_Hossler
08-09-2009, 10:27 AM
BECAUSE the .Found range has been altered, you must, must, must Collapse the range to its .End.
Thanks for this tip. Explains why some unexpected results in one of my macros
Paul
bstephens
11-24-2009, 11:38 PM
This would be a very helpful macro for me. But how can this macro be revised so that the text and brackets goes "red" when you click on the button once, but than is undone "not red" when you click on the button a second time?
Thanks you in advance for any ideas out there. Trying to get a hang of VBA in a hurry.
fumei
11-25-2009, 04:44 AM
Add a Public Boolean variable. Each time you execute the procedure, flip it.
Public bolYadda As Boolean
Sub DoIt()
If bolYadda = False Then ' NOT done
DoYourStuff
bolYadda = True ' now set because you ARE doing it
Else
DoYourUndoingStuff
bolYadda = False ' set False again
End If
End Sub
Or more specifically...
Public bolDone As Boolean
Sub MakeRed()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.MatchWildcards = True
.ClearFormatting
.Text = "\[*\]"
Do While .Execute(Forward:=True) = True
With r
.MoveStart Unit:=wdCharacter, Count:=1
.MoveEnd Unit:=wdCharacter, Count:=-1
If bolDone = True Then ' if true, then it IS red
.Font.Color = wdColorAutomatic
bolDone=False
' and now it is False...not red
Else
.Font.Color = wdColorRed
bolDone = True ' TRUE...it is red...now
End If
.Collapse 0
End With
Loop
End With
End Sub
This technique is standard for any sort of toggling operation.
bstephens
11-25-2009, 10:04 AM
Fumei, thank you for such a quick, detailed and helpful response!
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.