logo
search
VBA & Macro Problems

How to Highlight Complete Names in Word Using VBA Regular Expressions

Natalie TaylorNatalie Taylor Oct 13, 2026 869 views

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.

Highlight Complete Names in Word Using VBA Regular Expressions
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.
Before you start

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.

Solution 1Recommended

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.

1
Enable the RegExp Reference

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.

2
Initialize the RegExp Object

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.

3
Define the Regex Pattern

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]*\)".

4
Execute the Search and Loop Through Matches

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.

5
Apply Highlighting to the Document Range

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.

Use VBScript.RegExp Object to Find and Highlight Matches
Capitalization Matters: The provided regex pattern is case-sensitive and relies on proper capitalization of names and locations to work correctly. If your text contains uncapitalized names, you will need to adjust the [A-Z] constraints in the pattern.
Use WPS Writer for Advanced Document Macros

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. 1. Open WPS Writer: Launch WPS Office and open your target document.
  2. 2. Access Developer Tools: Navigate to the 'Developer' tab on the main ribbon to access macro features.
  3. 3. Open the VBA Editor: Click on 'VBA Editor' to open the scripting environment.
  4. 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. 5. Run the Macro: Execute the script to instantly highlight all complete names and locations in your document.
Fully compatible with Microsoft Word .docm files and VBA scripts.Lightweight, fast, and stable document processing.User-friendly Developer tools for intuitive macro management.Cost-effective solution for advanced document automation.
microsoft office alternative - wps office

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'.