logo
search
Document Editing Problems

How to Process an Entire Word Document with VBA Instead of One Page

Maira MehtabMaira Mehtab Sep 21, 2026 873 views

Question details

The user wants to run a VBA macro that processes and formats text across all pages of a Word document rather than stopping after the first page.

Product
Microsoft Word / WPS Writer
Device & OS
not provided
Scenario
Automating text search and formatting (such as applying bold, blue font, and yellow highlighting) using wildcards across a multi-page document.
Observed behavior
The current VBA script only processes a single page, requiring a method to loop through the entire document content to reach every page.
Before you start

Ensure you have saved a backup copy of your document before running any document-wide VBA macros, as bulk formatting changes applied via code can be difficult to undo manually.

Solution 1Recommended

Use ActiveDocument.Content with a VBA Loop

Set your search range to the entire document content and use a loop to find and process all matching instances.

When a VBA macro only affects the first page or current selection, it usually means the search range is restricted. By defining the range as ActiveDocument.Content, you instruct the macro to evaluate the entire document.

Adding a loop ensures the script continues to find matches until it reaches the end of the document. Implementing a maximum iteration limit is a highly recommended practice to prevent infinite loops during wildcard searches.

1
Open the VBA Editor

Press Alt + F11 in your word processor to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Go to Insert > Module in the top menu to create a blank script window for your document-wide macro.

3
Define the Search Range

In your VBA code, initialize your search object to span the whole document by setting your range variable to ActiveDocument.Content.

4
Implement the Search and Format Loop

Create a Do While loop using Find.Execute. Inside the loop, apply your desired formatting (e.g., bold text, font colors, or highlighting), collapse the range, and loop to search for the next match.

Avoid Infinite Loops: Always include a counter variable to set a maximum iteration limit (e.g., exit the loop if iterations exceed 5000) to prevent the macro from freezing your application if the wildcard pattern continuously matches the same text.
Automate Document Tasks in WPS Office

Use WPS Writer to Run Document-Wide Macros Seamlessly

WPS Office supports advanced macro functionality and VBA execution in its Professional and Business editions. You can easily automate repetitive formatting tasks, execute document-wide searches, and run wildcard text replacements just like in Microsoft Word.

  1. 1. Enable Developer Tools: Open WPS Writer, navigate to the 'Developer' tab on the ribbon, and ensure Macros are enabled for your document.
  2. 2. Access the Macro Editor: Click on 'Macros' or press Alt + F11 to launch the built-in VBA editor interface.
  3. 3. Paste and Run Your Script: Paste your ActiveDocument.Content VBA loop script into the module window and click 'Run' to apply the formatting across your entire WPS document.
Fully compatible with Microsoft Word (.docx, .doc, .docm) formats.Supports VBA macros for advanced document automation and document-wide formatting.Lightweight software design with a familiar, easy-to-navigate interface.Built-in tools for bulk text styling, highlighting, and formatting without coding.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA macro only format the first page of my Word document?

This happens if your macro's search range is limited to the current selection, view, or a specific page. You need to change the range to ActiveDocument.Content and use a loop to process the entire document.

How do I stop an infinite loop in a VBA macro?

If your macro gets stuck in an infinite loop while processing a document, you can press Ctrl + Break (or Ctrl + Pause) on your keyboard to interrupt the execution. To prevent this from happening entirely, add an iteration counter in your code that exits the loop after a realistic number of cycles.

Can I use wildcards in VBA to find and format text?

Yes, you can set Find.MatchWildcards = True in your VBA script. This allows you to search for complex patterns throughout the document and apply targeted formatting, like bold text or highlights, to every matching instance.

Do VBA macros written for Microsoft Word work in WPS Office?

Yes, WPS Office Professional and Business versions offer excellent VBA support, allowing you to run most standard Word macros, including those using ActiveDocument.Content, with little to no modification.