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

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:









































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

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

Comments (12)

  1. Shane Arthur says:

    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) when you guys reply? Thanks a million of you can.


  2. kns says:

    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!

  3. Stephanie34 says:

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

  4. asmith says:

    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!

  5. Liam Australia says:

    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


    End With


    End Sub

    1. Vazeer says:

      The above works well but the trouble is that it accepts only some 60+ words in the target list.
      How do I change this limit to my desired number of words?
      Thanks in advance.

  6. shawn says:

    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!

  7. shawn says:

    @ 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..

  8. Parm Singh says:

    Nice article, but all three links to previous document referred to in it are broken. The segments of sentences with broken hyperlinks are as follows:

    long ago we answered a question on how
    And you thought the Hey, Scripting Guy! archive was posted for your benefit
    a rehash of our previous column on boldfacing words

    It will be nice if these can be fixed.


    It will be helpful if yo

  9. yo says:

    How do i look for same word in word document

  10. Tom Guarnera says:

    Excuse the newbie question, but how do I run a Macro on the current document opened instead of running it against the file "C:ScriptsTest.doc". At the very least, how do I get the macro to prompt me for the location? I can see a prompt or against the
    current opened doc to be helpful, but pointing it to a specific filename isn’t helpful as I would have to move/rename the file each time I ran the macro.

Skip to main content