How to Highlight Parenthesized Text on Current Page with Word VBA
Question details
The user needs a Word VBA macro to search for specific string patterns (like 'Syn(10-4-3)') and highlight only the text or numbers within the parentheses, restricted to the current active page.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating text highlighting for specific patterns inside parentheses within a single page of a larger document.
- Observed behavior
- The goal is to restrict the macro's search scope strictly to the active page and automatically apply formatting (bold font and yellow highlight) to the matched parenthesized content.
Before running any VBA macro, ensure you test the script on a backup copy of your document, as macro operations typically cannot be undone via the standard Undo function.
Use the Built-in Page Bookmark in VBA
Create a VBA macro utilizing the built-in '\page' bookmark to limit the wildcard find-and-replace operation strictly to the current active page.
By default, a Word VBA search iterates through the entire document. To restrict the search area, you can set your search range to the predefined '\page' bookmark. Combined with wildcard search patterns, this allows you to target only the characters inside the parentheses and apply specific formatting like bolding and highlighting.
In your Word document, press the 'Alt + F11' keys on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the VBA editor, click on 'Insert' in the top menu bar and select 'Module' to create a blank script window for your new macro.
Begin your macro by defining the range to the current page. Use the code: 'Dim rng As Range' followed by 'Set rng = ActiveDocument.Bookmarks("\page").Range' to lock the search boundary.
Set up the 'rng.Find' parameters. Use 'rng.Find.Text = "\(*\)"' and ensure '.MatchWildcards = True' is enabled. Under the '.Replacement' property, set '.Highlight = True' and '.Font.Bold = True' to apply the yellow highlight and bold styling.
Close the VBA editor, place your cursor on the specific page you want to modify, and press 'Alt + F8'. Select your macro from the list and click 'Run'.

Automate Document Formatting with WPS Office
WPS Office fully supports VBA macros, allowing you to run custom scripts to highlight text, format documents, and automate repetitive tasks seamlessly.
- 1. Open your document: Launch WPS Writer and open the document you wish to format.
- 2. Enable the Developer Tab: Navigate to the top ribbon and click on the 'Tools' tab, then select 'Developer' to access macro features.
- 3. Launch the VBA Editor: Click 'Visual Basic Editor' to open the scripting environment.
- 4. Paste and Run Script: Insert a new module, paste your highlighting macro code, and run it directly within WPS Writer.

Frequently Asked Questions
How do I limit a Word VBA macro to search only the current page?
You can define the search range in your script by utilizing Word's predefined page bookmark. Setting your range variable to 'ActiveDocument.Bookmarks("\page").Range' restricts any subsequent find-and-replace actions to the page where your text cursor is currently located.
Why isn't my VBA macro finding text inside parentheses?
Parentheses are special characters in Word's search logic. To find them, you must use a wildcard search and escape the parentheses characters with backslashes (e.g., '\(*\)'). Also, ensure that the '.MatchWildcards = True' property is set in your Find operation.
Can I use VBA macros in WPS Office?
Yes, WPS Office provides excellent support for VBA macros. You can enable the Developer tools in WPS Writer to write, edit, and run VBA scripts for document automation, offering high compatibility with Microsoft Office macro-enabled files.




