Fix the VBA Loop Without Do Error in Word
Question details
The user needs to fix a compile error in their Word VBA macro preventing code execution.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Running a VBA macro that involves manipulating the ActiveDocument inside a loop.
- Observed behavior
- The macro execution stops immediately with a compile-time 'Loop without Do' error because a With block remains unclosed inside the loop structure.
Open your VBA Editor (Alt + F11) and locate the specific Do...Loop block where the compilation error is being triggered.
Close the With Block Using an End With Statement
This solution fixes the syntax structure by ensuring the With ActiveDocument block is properly closed before the loop repeats.
VBA reads control structures like Do loops and With blocks sequentially. If you start a With block inside a loop but forget to close it, VBA cannot map the Loop keyword back to the Do keyword, resulting in a 'Loop without Do' compilation error.
Press Alt + F11 to open the Visual Basic Editor and navigate to the module containing your macro.
Find the 'With ActiveDocument' statement that is nested inside your 'Do' loop.
Type 'End With' immediately above your 'Loop' keyword to properly terminate the With block.
Click Debug > Compile Project from the top menu to verify the syntax error is gone, then run your macro.

Debug SaveAs File Paths
After fixing the loop syntax, verify that the macro is generating valid file paths to prevent runtime failures during the SaveAs command.
Write and Run VBA Macros Seamlessly in WPS Office
WPS Office provides robust built-in support for VBA macros. You can easily open the VBA Editor, write your scripts, and debug loop structures in WPS Writer just as you would in Microsoft Word.
- 1. Open WPS Writer: Launch WPS Office and open your macro-enabled document.
- 2. Access the VBA Editor: Go to the Tools tab on the ribbon and click on the Macro button, then select Visual Basic Editor.
- 3. Edit Your Macro: Locate your script in the Project Explorer and type 'End With' above your 'Loop' statement to fix the syntax error.
- 4. Run the Code: Press F5 or click the Run button to execute the corrected macro directly within WPS Office.

Frequently Asked Questions
Why does VBA say 'Loop without Do' when I clearly have a Do statement?
VBA expects control blocks to be nested properly. If an inner block like an If statement or a With block is missing its closing statement (End If or End With), VBA gets confused. It attributes the structural failure to the next closing keyword it finds—in this case, the Loop keyword—making it seem like the Do statement is missing.
How can I prevent block structure errors in VBA programming?
Always indent your code consistently to visualize nested layers. Additionally, whenever you type an opening statement like Do, With, or If, immediately type its corresponding closing statement (Loop, End With, End If) on the next line before writing the internal logic.
Will an invalid file path cause a 'Loop without Do' error?
No. An invalid file path triggers a runtime error (like Error 52: Bad file name or number) only when the code is executed. 'Loop without Do' is a compile-time error caused strictly by incorrect syntax. However, once you fix the syntax, bad file paths are often the next hurdle in automation macros.




