Word's Find And Replace feature is handy, but it has certain limitations. Here's a macro that will let you highlight multiple search strings.
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:
- 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.
- 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.
- Enter a search string in the search control. I entered very (Figure B).
- 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.
- Close the dialog and you'll see that the highlights remain (Figure C).
Choose Advanced Find.
Enter a search string.
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.
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:
- Click the File tab and choose Save As in the left pane.
- Specify your folder location.
- In the Save As dialog, choose Word Macro-Enabled Document (*.docm) from the Save as type dropdown (Figure D).
- Click Save.
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:
- Press [Alt]+[F11] or click the Developer tab and click Visual Basic in the Code group to open the VBE.
- In the VBE's Project Explorer, select the appropriate document file (if necessary).
- Choose Module from the Insert menu.
- Enter the code using your preferred method.
- 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;
- Click the Developer tab.
- Click Macros in the Code group.
- In the resulting dialog, choose HighlightWords (Figure E) and click Run.
Run the macro.
Figure F shows the results.
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:
- Click the File tab and choose Options.
- Choose Customize Ribbon in the left pane.
- In the list to the right, check the Developer option (Figure G).
- Click OK.
Add the Developer tab to the ribbon.
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 firstname.lastname@example.org.
- Office Q&A: Juggling multiple indexes in a Word document
- Increase your productivity with Office and SharePoint add-ins
- Get the most out of your Excel PivotTables with these handy tips
- Reclaim Outlook categories for IMAP accounts