How to Highlight Complete Names in Word Using VBA Regular Expressions
Question details
The user wants to use a VBA script with regular expressions to find complete names followed by a location in parentheses and highlight the matched text in a Word document.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating text formatting and processing in a Word document by applying highlights to complex match patterns.
- Observed behavior
- The goal is to programmatically identify names with specific parenthetical locations (e.g., 'Joe Van Stepson (Matamata)') using RegExp, and apply a highlight color index to the corresponding Word range.
Ensure that the Developer tab is enabled in your Word ribbon and that you have added the 'Microsoft VBScript Regular Expressions 5.5' reference library to your VBA project before running the macro.
Use VBScript.RegExp Object to Find and Highlight Matches
Create a VBA macro that uses the VBScript RegExp object to parse text and apply highlighting to matches based on their starting index and length.
Word's built-in Find dialog does not natively support standard Regular Expressions to the extent needed for highly complex name extractions. To achieve this, you need to utilize the VBScript.RegExp object via VBA.
This method allows you to define complex patterns and manipulate the document's Range object based on the matched results, accurately highlighting only the designated text.
Open the VBA Editor by pressing Alt + F11. Go to 'Tools' > 'References' in the top menu, scroll down, and check the box for 'Microsoft VBScript Regular Expressions 5.5'. Click OK.
In a new module, declare your variables and create the RegExp object using code like: Dim regEx As Object, Set regEx = CreateObject("VBScript.RegExp"). Set regEx.Global = True to ensure it searches the entire document.
Assign the regex pattern to capture the name and parenthetical location. For example: regEx.Pattern = "([A-Z][a-z]+\s)+[A-Z][a-z]+\s\([A-Z][a-z]*\)".
Use 'Set matches = regEx.Execute(ActiveDocument.Content.Text)' to run the search. Create a 'For Each match In matches' loop to iterate through every found instance.
Inside the loop, define a Word Range based on the match's index. Use ActiveDocument.Range(Start:=match.FirstIndex, End:=match.FirstIndex + Len(match.Value)) and apply the highlight using Range.HighlightColorIndex = wdYellow.

Run VBA Macros to Highlight Text in WPS Office
WPS Office Writer provides robust support for VBA macros in its professional versions, allowing you to run your RegExp scripts seamlessly. Automate text highlighting and formatting tasks just as you would in Microsoft Word, all within a lightweight and highly compatible environment.
- 1. Open WPS Writer: Launch WPS Office and open your target document.
- 2. Access Developer Tools: Navigate to the 'Developer' tab on the main ribbon to access macro features.
- 3. Open the VBA Editor: Click on 'VBA Editor' to open the scripting environment.
- 4. Insert your Script: Paste your VBScript.RegExp highlighting script into a new module and ensure the Regular Expressions reference is enabled via the Tools menu.
- 5. Run the Macro: Execute the script to instantly highlight all complete names and locations in your document.

Frequently Asked Questions
Why doesn't Word's built-in wildcard search work for this regex?
Word's built-in Find and Replace uses a proprietary 'wildcard' syntax that is less powerful than standard Regular Expressions (RegExp). While you can do basic pattern matching, VBScript.RegExp handles complex groupings, quantifiers, and precise string extraction much more effectively.
How do I fix the 'User-defined type not defined' error in my VBA script?
This error typically occurs if the RegExp library is missing from your project references. In the VBA Editor, click 'Tools' > 'References' and ensure 'Microsoft VBScript Regular Expressions 5.5' is checked before running the macro.
Can I change the highlight color used in the macro?
Yes, you can easily change the highlight color by modifying the HighlightColorIndex property in your VBA script. You can replace 'wdYellow' with other Word color constants such as 'wdGreen', 'wdRed', or 'wdTurquoise'.




