Using wildcards in word macro

3.2k views Asked by At

I have created a template in Word 2007 with several fields that I want the users to be able to quickly find and edit. I have indicated those fields like so: [change this].

I want to create a macro which will find all words beginning with "[" and ending with "]" and highlight them.

My macro so far finds all words that start with "[" and highlights them. I just can't figure out how to have it highlight the rest of the word including "]".

I reviewed the rules for wildcards here http://www.gmayor.com/replace_using_wildcards.htm and thought I could use something like this: <[]> but it never finds anything.

I would appreciate some help! Thanks!

Here's the code:

Sub HighlightAllItems()
Dim myRange As Range
Dim searchText As String

searchText = "["

Set myRange = ActiveDocument.Range
With myRange.Find
.MatchWildcards = True
.Text = searchText
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Replacement.Text = searchText
.Forward = True
.Format = True
.Execute Replace:=wdReplaceAll
End With
End Sub
1

There are 1 answers

1
Alex Essilfie On BEST ANSWER

Try using this search pattern: \[<*>\]