logo
search
VBA & Macro Problems

How to Highlight Parenthesized Text on Current Page with Word VBA

Camila MilosovichCamila Milosovich Sep 25, 2026 872 views

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.

How to Highlight Parenthesized Text on the Current Page Using a Word VBA Macro
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 you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

In your Word document, press the 'Alt + F11' keys on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

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.

3
Define the Search Range

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.

4
Configure Wildcard Search and Formatting

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.

5
Execute the Macro

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

Use the Built-in Page Bookmark in VBA
Highlight Color Setting: To ensure the highlight applies as yellow, you may need to specify the default highlight color at the beginning of your script using 'Options.DefaultHighlightColorIndex = wdYellow'.
Powerful Macro Support

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. 1. Open your document: Launch WPS Writer and open the document you wish to format.
  2. 2. Enable the Developer Tab: Navigate to the top ribbon and click on the 'Tools' tab, then select 'Developer' to access macro features.
  3. 3. Launch the VBA Editor: Click 'Visual Basic Editor' to open the scripting environment.
  4. 4. Paste and Run Script: Insert a new module, paste your highlighting macro code, and run it directly within WPS Writer.
Fully compatible with Microsoft Word document formats (.docx, .doc, .docm).Robust built-in VBA and Macro support for advanced text automation.Free and lightweight office suite with a familiar user interface.
microsoft office alternative - wps office

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.