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

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

  • Comments 7
  • Likes
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 you thought the Hey, Scripting Guy! archive was posted for your benefit.)

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 our 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 ….

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • If I need to highlight a phrase, what's the best method of doing this?

    Ex. I need to search and highlight "I feel that"

    Using a method I learned where I create a seperate text files doesn't work because it searches and highlights the individual words. So, My questions is, can I put multi-word phrases into the arrWords array and have it search for the multi-word items?

    Thank you for your time. Since you guys don't use any "subscribe to comments" plugin which would inform me when you reply, would you mind sending me a little note to shanearthur (at) gmail.com when you guys reply? Thanks a million of you can.

    Shane

  • Hi ScriptingGuy1 and Team,

    How would you change this script so that we would be able to find the say 100 words listed in a .doc file and highlight each of them in different color? [Exception: In case we have fewer colors to choose then some words may use repeated colors]

    I hope we do not go in for the exception.

    I look forward your earliest reply!

    Thanks a lot!

  • It is easy to highlight many words in many word files using this small utility found on the net

    www.technocomsolutions.com/wordhighlighterhelp.htm

  • Most of my work is looking for changes in a document, and then highlighting them. These changes are indicated in the document as underlined, or strikethrough. Is there a way to search for these types of changes, and highlight them, instead of acutal words? I have been searching online and cannot find anything yet. Hoping you guys could help.

    thanks much!

  • You should run this macro code within MS Word. I found this on another site. It works great. Simply change the words or text you need. Mine are 'must, will and shall'.

    Sub HighlightTargets2()

    Dim range As range

    Dim i As Long

    Dim TargetList

    TargetList = Array("must", "will", "shall") ' put list of terms to find here

    For i = 0 To UBound(TargetList)

    Set range = ActiveDocument.range

    With range.Find

    .Text = TargetList(i)

    .Format = True

    .MatchCase = True

    .MatchWholeWord = False

    .MatchWildcards = False

    .MatchSoundsLike = False

    .MatchAllWordForms = False

    Do While .Execute(Forward:=True) = True

    range.HighlightColorIndex = wdYellow

    Loop

    End With

    Next

    End Sub

  • Hey Scripting Guys and Folks in the comments :)

    I have many words (say approx 25-30 words) to highlight and I need different colors for each of them, preferably light colors.. so I could read these words after highlighting.

    Now I cannot use HighlightColorIndex in VBA coding as it limits with very few colors..

    How do I achieve this in vba coding for MS Word? I searched internet and found that we could possibly use RGB values.. like shading option, but could not find a way to do it. I mean, the text fill or shading tool has many colors. how can we use these in VBA.

    Another note: suggestions using find and replace would be best, because when I use arrays in my code.. I could not include special characters such as ( " ) double quotation marks or $ % or ® ™ together with using wildcards for some.

    User Interface: MS Word 2007 in Windows 7

    Thanks in advance!

  • @ Stephanie - 25 Jul 2011 11:53 AM

    the tool you have suggested has highlight color limitation, you can find that in their demo video, as the drop down menu shows "No Color" for additional colors..