How to Use a Word VBA Macro to Search and Highlight Multiple Words
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.

- 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.
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.
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.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor window.
Click 'Insert' from the top menu bar and select 'Module' to create a blank scripting window.
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.
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.
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.

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. Enable Developer Tools: Open WPS Writer, go to the Options menu, and ensure the Developer tab is enabled to access macro features.
- 2. Access the VBA Editor: Click on the Developer tab on the ribbon and select 'Visual Basic', or simply press Alt + F11.
- 3. Execute Your Script: Insert a new module, paste your array loop and paragraph shading code, and run it to instantly format your document.

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.




