Fix Word VBA Search Macro Not Stopping at End of Document
Question details
The user needs to prevent a Word VBA wildcard search macro from getting stuck in an infinite loop when it reaches the end of the document.
- Product
- Word
- Device & OS
- not provided
- Scenario
- Running a VBA macro to search for names using wildcards and applying formatting to the matched text.
- Observed behavior
- The macro formats the text but fails to stop at the end of the document. Commands like Selection.MoveDown and Selection.HomeKey push the selection beyond the document range, causing the loop to freeze or run indefinitely.
Press Ctrl + Break (or Pause) on your keyboard to interrupt the currently frozen macro, and save a backup of your document before modifying the VBA code.
Use a Range Object Instead of the Selection Object
By defining a specific Range object and collapsing it after each match, you can restrict the wildcard search to the document's actual content and prevent infinite loops.
Relying on Selection.MoveDown or Selection.HomeKey to navigate through a document within a search loop often causes the macro to lose track of the document's boundary. This results in the macro searching the same text repeatedly or trying to move past the final character.
To solve this reliably, use a Range object. A Range represents a contiguous area in the document and is independent of the cursor. When a match is found and formatted, you can collapse the Range to its endpoint, forcing the next search to begin exactly where the last one finished.
Press Alt + F11 in Word to open the Microsoft Visual Basic for Applications window, and locate the module containing your looping macro.
At the beginning of your macro, add 'Dim rng As Range' and set it to your desired search area, for example: 'Set rng = ActiveDocument.Content' to search the entire document.
Replace 'Selection.Find' with 'rng.Find'. Set up your wildcard parameters such as 'rng.Find.MatchWildcards = True' and specify the search text.
Use a 'Do While rng.Find.Execute' loop. Inside the loop, apply your formatting to the 'rng' object (e.g., 'rng.Font.Bold = True').
Critically, at the end of the loop right before 'Loop', add the line 'rng.Collapse wdCollapseEnd'. This moves the start of the search range to immediately after the found text, ensuring the loop eventually reaches the end of the document and stops naturally.
Format Text Effortlessly with Macros in WPS Writer
WPS Office fully supports VBA macros, allowing you to run, edit, and debug scripts seamlessly. You can easily fix looping issues and automate complex wildcard searches within the built-in WPS Macro Editor.
- 1. Launch WPS Writer: Open your document in WPS Writer and navigate to the 'Tools' tab on the ribbon.
- 2. Open the Macro Editor: Click on 'Macros' and select 'Macro Editor', or simply press Alt + F11 to launch the VBA environment.
- 3. Paste and Run Your Script: Paste your Range-based VBA script to handle the wildcard formatting. Run the macro to automatically process all names and associated numbers without getting stuck.

Frequently Asked Questions
Why does my Word VBA search macro freeze at the end of the document?
It freezes because methods like Selection.Find paired with Selection.MoveDown can push the cursor past the document's end. When this happens, the loop condition (Do While .Execute) remains true but the cursor cannot advance, causing an infinite loop.
How do I collapse a Range in Word VBA?
You can collapse a Range by using the Collapse method on your Range object and passing the wdCollapseEnd parameter (e.g., rng.Collapse Direction:=wdCollapseEnd). This moves the start point of the range to the very end of the currently matched text.
Can a VBA macro select both names and associated numbers using wildcards?
Yes. You can expand your wildcard search string within the .Text property of your Find object (for example, using '[0-9]{1,}' to denote numbers) to capture and format names along with their adjacent numbers in a single pass.
What is the difference between Range and Selection in Word VBA?
A Selection represents the actual highlighted text or cursor position in the user interface, which is slower and prone to screen updating errors. A Range is an invisible object referencing a specific area in the document, making it faster and much more reliable for macros.




