Word’s Find And Replace feature is flexible, but it can become tedious if you routinely search for the same strings. For example, let’s suppose you’re an editor and your publisher has a list of overused words you remove from all manuscripts. You can do it the hard way, finding each word one by one, reviewing for context, and then deleting (or not). Or you can use a macro to highlight all instances of all these words and phrases at once. Combining all of the search terms into one find task is efficient. Highlighting each instance allows you the luxury of reviewing each within context.

In this article, you’ll learn how to use Find And Replace to highlight all instances of a single search term. Next, you’ll review a macro that highlights all instances of several search terms at the same time.

I’ll be using Word 2016 on a Windows 10 64-bit system. The differences in the Ribbon version are insignificant. Where notably different, I’ve included some Word 2003 instructions. You can use your own document or download the example .docm, .doc, and. bas files.

Manually highlighting a search string

When you click Find to open the Navigation pane, the feature will highlight all instances of a search string. Word will clear the highlights when you close the Navigation pane. This might be exactly what you need. If you want to retain the highlights, use the Advanced Find option instead. To demonstrate this technique, let’s highlight all instances of the word very in the example document, as follows:

  1. On the Home tab, choose a highlight from the Text Highlight Color option if you want to specify the highlight or if the option has been cleared. This feature relies on the currently selected highlight.
  2. Choose Advanced Find from the Find dropdown in the Editing group on the Home tab (Figure A). Don’t use Find or Replace to open the Navigation pane. In Word 2003, choose Find from the Edit menu.
  3. Enter a search string in the search control. I entered very (Figure B).
  4. Click Reading Highlight and choose Highlight All and Word will highlight all instances of very. In Word 2003, check Highlight All Items Found In and click Find All. Using the Find In options, you can hone the search to specific sections, but Main Document is the only choice in the example.
  5. Close the dialog and you’ll see that the highlights remain (Figure C).

Figure A

Choose Advanced Find.

Figure B

Enter a search string.

Figure C

The highlights remain after you close the dialog, but they’re temporary.

To clear the highlights, use Advanced Find and choose Clear Highlighting from the Reading Highlight dropdown. If you delete any instance of a highlighted search string, Word will delete all the remaining highlights. Pressing [Ctrl]+Z will reclaim a deleted string but not the highlights. Word won’t save the highlights; once you close the document, the highlights are gone.

This trick can come in handy during a meeting or presentation when someone asks for more details, future plans, and so on. Simply highlight a specific word or phrase to quickly find key points that you can expound upon using the surrounding content.

Macro highlights multiple search strings

The manual way is fine and it might be all you need. However, it’s limited: You can search for only one string and the highlights are temporary. If you routinely search for the same list of words and phrases and you want a more permanent highlight, you can use the macro in Listing A.

Listing A

Sub HighlightWords()

Dim Word As Range

Dim WordCollection(2) As String

Dim Words As Variant

'Define list.

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

WordCollection(0) = "very"

WordCollection(1) = "just"

WordCollection(2) = "of course"

'Set highlight color.

Options.DefaultHighlightColorIndex = wdYellow

'Clear existing formatting and settings in Find feature.

Selection.Find.ClearFormatting

Selection.Find.Replacement.ClearFormatting

'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

Next

Next

End Sub

Note: Don’t copy and paste the code from this web page, because the Visual Basic Editor (VBE) rejects some web-based characters. You can find the macro in the downloadable example file or you can import the module file (also in the download) into your own Word document.

If you’re working with a Ribbon version of Word, save your file as a macro-enabled file as follows:

  1. Click the File tab and choose Save As in the left pane.
  2. Specify your folder location.
  3. In the Save As dialog, choose Word Macro-Enabled Document (*.docm) from the Save as type dropdown (Figure D).
  4. Click Save.

Figure D

You must save your Ribbon version document as a macro-enabled document; Word 2003 users can skip this step.

Now you’re ready to enter the sub procedure (macro) into the VBE, as follows:

  1. Press [Alt]+[F11] or click the Developer tab and click Visual Basic in the Code group to open the VBE.
  2. In the VBE’s Project Explorer, select the appropriate document file (if necessary).
  3. Choose Module from the Insert menu.
  4. Enter the code using your preferred method.
  5. Return to the document and save it.

How the macro works

The procedure relies on a string array, as declared by

Dim WordCollection(2) As String

to store each word or phrase you want to highlight. The argument is the value 2. To customize this collection for your own list, this value must be one less than the number of words in your collection (collections start with the index value of 0). In our example, the following statements add three search strings to the WordCollection array:

WordCollection(0) = “very”
WordCollection(1) = “just”
WordCollection(2) = “of course”

This is where you’ll add your own search terms when customizing the macro for your own use.

Because Word’s Find And Replace feature remembers your last settings, the next two statements clear previous settings. Then, the statement

Options.DefaultHighlightColorIndex = wdYellow

sets the highlight color. If you omit this statement, Word defaults to the current setting, which might be no highlighting at all. You can change this to any highlight constant you want.

The first For Each statement cycles through the document. The second cycles through the WordCollection collection. The

.Format = True

statement is VBA’s version of specifying a format in Word’s Replace With option. Most of the other options aren’t strictly necessary, but by explicitly setting them you avoid unexpected results. The macro evaluates the entire document, so it doesn’t matter where your cursor is when you execute the macro.

Running the macro

Now, let’s run the macro and see what happens. Remember, our collection includes the three search strings very, just, and of course. To run the macro, do the following;

  1. Click the Developer tab.
  2. Click Macros in the Code group.
  3. In the resulting dialog, choose HighlightWords (Figure E) and click Run.

Figure E

Run the macro.

Figure F shows the results.

Figure F

The macro highlights the search strings.

When you’re ready to clear the highlighting, you can do so as you normally would.

No Developer tab?

If the Developer tab isn’t available, display it as follows:

  1. Click the File tab and choose Options.
  2. Choose Customize Ribbon in the left pane.
  3. In the list to the right, check the Developer option (Figure G).
  4. Click OK.

Figure G

Add the Developer tab to the ribbon.

Easy access

You can add the sub procedure (as a macro) to the Quick Access Toolbar (QAT) or create a custom tab for your macros. Read Two easy ways to customize the Ribbon interface for instructions on doing so. If you’re using Word 2003, read Add a custom macro menu to Word.

Send me your question about Office

I answer readers’ questions when I can, but there’s no guarantee. When contacting me, be as specific as possible. For example, “Please troubleshoot my workbook and fix what’s wrong” probably won’t get a response, but “Can you tell me why this formula isn’t returning the expected results?” might. Please mention the app and version that you’re using. Don’t send files unless requested; initial requests for help that arrive with attached files will be deleted unread. I’m not reimbursed by TechRepublic for my time or expertise when helping readers, nor do I ask for a fee from readers I help. You can contact me at susansalesharkins@gmail.com.

Also read…

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays