How to Stop a Find Loop at the Last Occurrence in Word VBA
Question details
The user needs to stop a Word VBA macro Find loop correctly after locating the final occurrence of a specific text format (e.g., font size 20) and changing the preceding character.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Running a VBA macro script to automate finding and modifying specific text formats throughout a document.
- Observed behavior
- The macro may enter an infinite loop at the end of the document, or trigger a 'Loop without Do' compile error if the code structure is incorrect.
Ensure you have saved a backup copy of your document before running or testing new VBA macros to prevent accidental data modifications.
Use Document Range Tracking in a Do Loop
The most reliable way to prevent an infinite loop is to store the starting document range and exit the loop when the search fails or returns to the original range.
Relying solely on page numbers or the Selection.Find.Found property can cause infinite loops, especially if valid matches exist on the last page of the document. Tracking the document range ensures the macro accurately knows when it has processed the entire document.
Define and store the starting document range at the beginning of your VBA script using a Range object variable.
Set up a Do...Loop structure to search for the next occurrence of your target criteria, such as font size 20.
Add a backward search within the loop to locate and modify the preceding character (e.g., changing the pipe character to black).
Include an If statement to exit the loop (Exit Do) when Selection.Find.Found evaluates to False or if the current search range intersects with the original starting range.

Fix VBA Loop Syntax Errors
Resolve 'Loop without Do' compile errors by checking the structure of your If blocks and Loop statements.
Execute and Debug VBA Macros in WPS Writer
WPS Office provides excellent support for VBA macros, allowing you to run, edit, and debug your document automation scripts with ease.
- 1. Open your document: Launch WPS Writer and open the document containing your target text.
- 2. Access the Developer tab: Navigate to the Developer tab on the top ribbon to access macro tools.
- 3. Open the VBA Editor: Click on 'Macros' or 'Visual Basic' to open the integrated VBA Editor.
- 4. Insert your macro code: Paste or edit your Find loop macro code in the correct module.
- 5. Run the macro: Execute the macro to automatically process your document formatting and stop at the last occurrence.

Frequently Asked Questions
Why does my Word VBA Find loop run endlessly?
An endless loop occurs when the search criteria loop back to the beginning of the document or get stuck on the last found item without a proper exit condition. Using range tracking ensures the loop exits once it passes the final occurrence.
What does the 'Loop without Do' compile error mean in VBA?
This error usually happens when an If statement inside a Do...Loop is not properly closed with an End If before the Loop command is reached, disrupting the code structure.
Can I use page numbers to stop a macro at the end of a document?
While you can track the active page using Selection.Information(wdActiveEndPageNumber), it is not recommended for stopping Find loops. Doing so might cause the macro to skip valid matches located on the final page.




