logo
search
Office Settings & Configuration

How to Use a Word VBA Macro to Search and Highlight Multiple Words

Guest WriterGuest Writer Sep 27, 2026 871 views

Question details

The user wants to search for multiple specific terms in a document and apply background shading to the entire paragraph containing those terms using a VBA macro.

How to Use a Word VBA Macro to Search and Highlight Multiple Words
Product
Microsoft Word / WPS Writer
Device & OS
not provided
Scenario
Automating the process of finding specific text strings and visually highlighting their respective paragraphs for review or formatting purposes.
Observed behavior
Needs a programmatic solution (VBA macro) to loop through an array of search terms (e.g., .ATOD., .HE., .MEH.) and apply a pale blue background to any matched paragraph.
Before you start

Ensure that your document is saved and that you have enabled the Developer tab in your word processor's ribbon to access the Visual Basic for Applications (VBA) editor.

Solution 1Recommended

Create and Run a VBA Macro to Loop and Highlight

Use a custom VBA script with an array to define search words, loop through the document, and apply background shading to paragraphs where matches are found.

This method utilizes the Find object within a VBA loop. By defining an array of text strings, the macro searches the document for each term sequentially and alters the background color of the specific paragraph containing the match.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor window.

2
Insert a New Module

Click 'Insert' from the top menu bar and select 'Module' to create a blank scripting window.

3
Define the Array and Loop

Write a VBA script that declares an array of your search terms, for example: `MyTerms = Array(".ATOD.", ".HE.", ".MEH.")`. Use a `For` loop to iterate through each term in the array.

4
Apply Paragraph Shading

Within the loop, use the `Selection.Find.Execute` method. When a match is found, add the line `Selection.Paragraphs(1).Shading.BackgroundPatternColor = wdColorPaleBlue` to highlight the entire paragraph.

5
Run the Macro

Press F5 or click the green 'Run' button on the toolbar to execute the script. The specified paragraphs in your document will instantly be shaded.

Create and Run a VBA Macro to Loop and Highlight
Macro Security Settings: Make sure your macro security settings allow the execution of local scripts. You may need to enable macros in your Trust Center settings before the code can run.
Advanced Document Processing

Run Macros and Automate Tasks Seamlessly in WPS Writer

WPS Office provides robust support for VBA macros, allowing you to automate repetitive tasks like searching and highlighting multiple terms with ease. Enjoy a highly compatible and efficient word processing experience.

  1. 1. Enable Developer Tools: Open WPS Writer, go to the Options menu, and ensure the Developer tab is enabled to access macro features.
  2. 2. Access the VBA Editor: Click on the Developer tab on the ribbon and select 'Visual Basic', or simply press Alt + F11.
  3. 3. Execute Your Script: Insert a new module, paste your array loop and paragraph shading code, and run it to instantly format your document.
Fully compatible with Microsoft Word VBA scripts and .docm formatsLightweight application with high performance for executing automated scriptsFamiliar ribbon interface makes finding the Developer tools easySeamlessly batch-format paragraphs and automate complex workflows
microsoft office alternative - wps office

Frequently Asked Questions

How can I change the highlight color in the VBA macro?

You can change the color by modifying the `wdColorPaleBlue` constant in your VBA code to another built-in Word color constant, such as `wdColorYellow` or `wdColorBrightGreen`.

Why isn't my macro highlighting the entire paragraph?

Ensure your code specifies `Selection.Paragraphs(1).Shading.BackgroundPatternColor`. If you only apply formatting to `Selection.Range.HighlightColorIndex`, it will only highlight the specific matched word rather than the entire paragraph block.

Can I run this highlight macro automatically when the document opens?

Yes, you can rename your macro to `Document_Open()` and place it in the 'ThisDocument' module in the VBA editor. Note that users will need to enable macros when opening the file for it to trigger.

Does WPS Office support VBA macros?

Yes, advanced versions of WPS Office support VBA macros. Depending on your version, you may need to ensure you have the VBA module installed to run customized `.docm` files.