March 21st, 2006

How Can I Search for and Highlight Words in a Microsoft Word Document?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I search a Word document for any of 305 different words and highlight these words if they are found?

— SB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SB. This might be hard for you to believe, but the rest of the technical writing community doesn’t exactly hold the Scripting Guys in awe. However, most of our fellow writers do grudgingly concede that writing a daily scripting column is a noteworthy endeavor. “How do you do that?” they ask. “How do you find the time to write a brand-new column each and every day?”

Needless to say, the Scripting Guys reply with a very thoughtful monologue about the values of hard work, dedication, and devotion both to Microsoft and to Microsoft’s customers. But you guys know better: you’d like to know how we really manage to write a brand-new column each day. Well, just between you and us, here’s the secret: we don’t actually write a new column each day. It just looks like it.

If you ever decide to write a daily scripting column, our advice would be to write one column and then figure out how to keep writing that same column over and over again. For example, not too long ago we answered a question on how to search a Word document for a specific word and, if found, boldface that word. Now, someone who really was hardworking and dedicated would write that column and then forgot about it, moving on to the next challenge. But not the Scripting Guys. We saved that column, then waited for the opportunity to use it all over again.

And, lo and behold, here’s our opportunity. You need to search for any of 305 different words and then highlight those words if found? Well, we won’t actually show a script that uses 305 different words. But here’s a script that searches for two words – boldface and the – and highlights either of those words if found:

Const wdReplaceAll = 2

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")
Set objSelection = objWord.Selection

arrWords = Array("boldface", "the")

For Each strWord in arrWords
    objSelection.Find.Text = strWord
    objSelection.Find.Forward = TRUE
    objSelection.Find.MatchWholeWord = TRUE

    objSelection.Find.Replacement.Highlight = TRUE

    objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
Next

Like we said, this column is largely a rehash of a previous column on boldfacing words in a Word document. Because of that we’ll skip much of the explanation, and focus on the differences between this script and the boldfacing script. If you don’t fully understand the basic workings of this script (for example, how you conduct a find-and-replace operation in Microsoft Word in the first place) please see the boldfacing column for more details.

Just like the previous column, the script in this column starts out by creating a constant named wdReplaceAll and setting the value to 2; we’ll use this constant to tell Word to automatically replace all instances of the target text. We then create a visible instance of the Word.Application object and use the Open method to open the document C:\Scripts\Test.doc. After creating an instance of the Word Selection object we’re ready to roll up our sleeves and get to work.

Our first step is to define the words we want to search for. In this case we’ve decided to search for only two words, so we simply store both of those words in an array named arrWords:

arrWords = Array("boldface", "the")

Note. If you wanted to you could create an array containing 305 words. Alternatively, it might be easier to simply list the words in a text file and then have the script read that text file line-by-line and then search for the words one at a time. That’s something we’ll leave up to you.

Next we set up a For Each loop to loop through this collection of words. Inside the loop we assign the first word in the array to the Text property of the Find object. Those two lines of code look like this:

For Each strWord in arrWords
    objSelection.Find.Text = strWord

After defining the search text we configure a few additional properties of the Find object, including the Forward and MatchWholeWord properties. The Forward property tells the script that we want the search to start at the beginning of the document (the default location for the Selection object) and continue forward through the end of the document. The MatchWholeWord property tells the script to match only exact instances of boldface and the; that way we don’t end up highlighting words like them, anthem, or anything else containing the letters t-h-e.

Of course, we don’t just want to find the target words; we also want to highlight those words as we find them. To do that all we have to do is set the value of the Replacement object’s Highlight property to True:

objSelection.Find.Replacement.Highlight = TRUE

That’s basically all we have to do. From here we call the Execute method, which searches for and highlights all instances of the word boldface. The script then loops around and does the same thing for all instances of the second word in our array (the). This continues until we have searched for and highlighted all the words in our array, regardless of whether we have two words in the array or 302 words in the array. (And yes, it will work even if we have 305 words in the array.)

And what do we end up with when the script grinds to a halt? We end up with this:

Hey, Scripting Guy!

 

Note. As it turns out, Test.doc happens to be the column on boldfacing words in a Word document. The Scripting Guys really are dedicated to recycling!

Of course, you might be thinking, “Well, that’s nice. But I just don’t like that yellow highlighting. If only there was a way to use turquoise highlighting instead.”

Hey, all you had to do was ask. It’s actually quite easy to assign a different color to your highlighter. To do that, start by picking a constant and value from the following table:

Constant

Value

wdAuto

0

wdBlack

1

wdBlue

2

wdBrightGreen

4

wdByAuthor

-1

wdDarkBlue

9

wdDarkRed

13

wdDarkYellow

14

wdGray25

16

wdGray50

15

wdGreen

11

wdNoHighlight

0

wdPink

5

wdRed

6

wdTeal

10

wdTurquoise

3

wdViolet

12

wdWhite

8

wdYellow

7

Somewhere near the start of your script define that constant, assigning it the corresponding value:

Const wdTurquoise = 3

Then, after you’ve created an instance of the Word.Application object, create an instance of the Options object, assigning the chosen constant to the DefaultHighlightColorIndex property:

Set objOptions = objWord.Options
objOptions.DefaultHighlightColorIndex = wdTurquoise

The completed script should look something like this:

Const wdReplaceAll = 2
Const wdTurquoise = 3

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objOptions = objWord.Options
objOptions.DefaultHighlightColorIndex = wdTurquoise

Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")
Set objSelection = objWord.Selection

arrWords = Array("boldface", "the")

For Each strWord in arrWords
    objSelection.Find.Text = strWord
    objSelection.Find.Forward = TRUE
    objSelection.Find.MatchWholeWord = TRUE

    objSelection.Find.Replacement.Highlight = True
    objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
Next

This will result in all the target words being highlighted with a turquoise highlight. Is it possible to mix highlighter colors in a single document? Yes, but that will have to be a topic for another day.

Now, if someone would just write in and ask how to search for and italicize words in a Microsoft Word document we’d be in business. And if someone else would ask how to underline those words, well ….

Author

0 comments

Discussion are closed.