How to Loop Through Numbered Paragraphs in Word VBA
Question details
The user needs a VBA macro that loops through specific numbered paragraphs, inserts page breaks, and allows dynamic selection of the starting paragraph and total page count.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating document formatting by iterating through numbered lists and prompting the user for input parameters before execution.
- Observed behavior
- Instead of a hardcoded loop like 1 to 30, the user wants the macro to use InputBoxes for variable inputs and successfully find paragraphs without stopping after the first match.
Ensure your document uses proper automatic numbering for paragraphs and that you have enabled the Developer tab to access the Visual Basic editor.
Use InputBox Variables and a Dynamic For Loop in VBA
Modify your VBA macro to use variables from InputBox prompts to determine loop boundaries, ensuring search parameters are reset for every iteration.
To process a dynamic range of numbered paragraphs (such as paragraphs 2 to 24), you should replace hardcoded integers in your For loop with variables. Utilizing InputBox functions allows the user to specify the starting paragraph and the required number of pages directly upon running the macro.
Open the VBA Editor (Alt + F11) and declare integers for your start parameter, end parameter, and the loop counter.
Use `startPara = InputBox("Enter starting paragraph:")` and `pageCount = InputBox("Enter required number of pages:")` to capture user input.
Set the loop range to evaluate the variables, for example, `For i = startPara To (startPara + pageCount - 1)`.
Inside the loop, ensure that variables like `found` or your `Selection.Find` range are reset so that consecutive iterations search the whole document rather than the end of the previous find.
Use the Find method to search for the current paragraph number `i`. Once located, apply `Selection.InsertBreak Type:=wdPageBreak` to execute the formatting.

Use WPS Writer for Advanced VBA Automation
WPS Office includes robust VBA and macro support, allowing you to easily run customized scripts to loop through numbered paragraphs, automate page breaks, and streamline complex document formatting.
- 1. Download and Install WPS Office: Get the latest version of WPS Office and ensure the VBA component is installed.
- 2. Open Your Document: Launch WPS Writer and open the document containing your numbered paragraphs.
- 3. Access the Developer Tab: Navigate to the Developer tab on the top ribbon and click on 'Visual Basic' to open the code editor.
- 4. Run Your Macro: Paste your dynamic loop script into a module and click 'Run' to apply the formatting changes instantly.

Frequently Asked Questions
Why does my VBA InputBox return an error when calculating loop bounds?
InputBox returns a string by default. When using it to calculate loop boundaries, you may need to use the `Val()` or `CInt()` functions to convert the string input into an integer.
How do I stop my VBA loop if the user clicks Cancel on the InputBox?
If a user clicks Cancel, the InputBox returns an empty string. You can add an `If startPara = "" Then Exit Sub` check immediately after the InputBox line to safely terminate the macro.
Is it possible to search for automatic list numbers using VBA?
Searching for automatic numbering can be tricky because the numbers are not standard text characters. You often need to iterate through the `Paragraphs` collection and check the `Range.ListFormat.ListString` property to match specific list numbers.




