How to Highlight Only Numbers Inside Parentheses Using Word VBA
Question details
The user needs to format only the numbers enclosed in parentheses (e.g., "28-0-7") after finding a complete expression (e.g., "Jump(28-0-7)") using a wildcard search in Word VBA.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- Running a VBA macro to perform a wildcard search and apply specific formatting to a substring of the matched text without affecting the prefix or parentheses.
- Observed behavior
- The wildcard search captures the entire string including the prefix and parentheses, making it difficult to format only the target numeric substring inside.
Ensure you have enabled the Developer tab in your Word processor to access the VBA editor. It is also highly recommended to save a copy of your document before running new macros to prevent unintended formatting changes.
Adjust Range Boundaries to Target the Numbers
Duplicate the found range and manually shift the start and end positions to exclude the prefix and parentheses before applying formatting.
When a wildcard search matches a full pattern like 'Jump(28-0-7)', applying formatting to the default search range affects the whole string. By creating a duplicate range object, you can programmatically move the start and end points inward to isolate just the numbers.
Set up your standard VBA Find operation with '.MatchWildcards = True' to locate the target expression. Ensure your find loop correctly identifies the full string.
Inside your execution loop, declare a new Range variable and set it equal to the found range using a command like 'Set rngFormat = rngFound.Duplicate'.
Move the duplicate range's start position forward past the prefix and opening parenthesis (e.g., 'rngFormat.Start = rngFormat.Start + 5'). Then, move the end position backward by one to exclude the closing parenthesis ('rngFormat.End = rngFormat.End - 1').
Apply your desired text properties to the adjusted range variable. For example, use 'rngFormat.HighlightColorIndex = wdYellow' and 'rngFormat.Font.Bold = True' to highlight and bold the numbers.

Use WPS Writer to Run VBA Macros and Automate Tasks
WPS Office provides excellent compatibility with Microsoft Word documents, including support for VBA macros in its advanced editions. You can seamlessly run your macro scripts to format specific text patterns, like highlighting numbers in parentheses.
- 1. Open WPS Writer: Launch WPS Office and open your document in WPS Writer.
- 2. Access the Developer Tab: Go to the Developer tab on the top ribbon and click on the 'Visual Basic' icon to open the built-in VBA editor.
- 3. Insert and Run Your Macro: Paste your range-adjusting VBA script into a new module and click 'Run' to instantly format the targeted numbers inside parentheses across your document.

Frequently Asked Questions
Why does my Word VBA wildcard search select the whole word instead of just the numbers?
A wildcard search selects the entire string that matches the pattern you define. If your pattern includes the prefix and parentheses to ensure context accuracy, the entire matched expression is selected by default. You must adjust the range object in VBA to isolate the inner numbers.
How do I move a VBA range start position forward?
You can adjust the start of a range by manipulating its Start property. For example, typing 'myRange.Start = myRange.Start + 5' moves the starting point 5 characters to the right, effectively skipping over a 4-letter prefix and an opening parenthesis.
Will adjusting the range in VBA break my Find loop?
It will break the loop if you alter the original search range. That is why you must create a duplicate range (e.g., 'Set newRange = searchRange.Duplicate') and apply your boundary adjustments and formatting exclusively to the duplicate, leaving the original search range intact for the Find loop.
Can I use RegEx instead of wildcards in Word VBA to extract the numbers?
Yes, you can use Regular Expressions by adding a reference to 'Microsoft VBScript Regular Expressions 5.5' in the VBA editor. RegEx allows for capturing groups, which can sometimes be more flexible than standard Word wildcard range manipulation for complex patterns.




