top of page

highlight multiple strings in a Word document

If you want to highlight multiple strings in a Word document, you can do so by utilizing a simple macro posted here, by Susan Harkins with Tech Republic.

In this example we have a document in which we want to search for any one of five terms.

Edit the macro, posted below, so that the strings or words we want to highlight are listed one by one in the lines beginning 'WordCollection'. Set the number for each of these things in the parentheses after 'WordCollection' so that each term is successively numbered, but begin with zero:

On the third line set the number after 'Dim WordCollection' in parentheses to the match number of terms you're searching for. To be clear, if the last term is preceded by 'WordCollection(4)' set the number on the third line of the visual basic code to 5.

You can set the color of the highlighting on the first operative line after those in which the searched for terms are listed.

The macro does take a good deal of time to run. If you are searching for multiple strings in a long document, it may take several minutes to finish running.

Searched for strings which appear as part of longer words will be highlighted.

As always, this was tested and confirmed to work correctly by Litigation Support Tip of the Night.

Sub HighlightWords()

Dim Word As Range

Dim WordCollection(5) As String

Dim Words As Variant

'Define list.

'If you add or delete, change value above in Dim statement.

WordCollection(0) = "whale"

WordCollection(1) = "Ahab"

WordCollection(2) = "sailor"

WordCollection(3) = "sea"

WordCollection(4) = "ivory"

'Set highlight color.

Options.DefaultHighlightColorIndex = wdPink

'Clear existing formatting and settings in Find feature.



'Set highlight to replace setting.

Selection.Find.Replacement.Highlight = True

'Cycle through document and find words in collection.

'Highlight words when found.

For Each Word In ActiveDocument.Words

For Each Words In WordCollection

With Selection.Find

.Text = Words

.Replacement.Text = ""

.Forward = True

.Wrap = wdFindContinue

.Format = True

.MatchCase = False

.MatchWholeWord = False

.MatchWildcards = False

.MatchSoundsLike = False

.MatchAllWordForms = False

End With

Selection.Find.Execute Replace:=wdReplaceAll



End Sub


bottom of page