logo
search
VBA & Macro Problems

How to Loop Through Numbered Paragraphs in Word VBA

Bushra ParveenBushra Parveen Oct 9, 2026 869 views

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.

How to Loop Through Numbered Paragraphs in Word VBA
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.
Before you start

Ensure your document uses proper automatic numbering for paragraphs and that you have enabled the Developer tab to access the Visual Basic editor.

Solution 1Recommended

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.

1
Declare Macro Variables

Open the VBA Editor (Alt + F11) and declare integers for your start parameter, end parameter, and the loop counter.

2
Add InputBox Prompts

Use `startPara = InputBox("Enter starting paragraph:")` and `pageCount = InputBox("Enter required number of pages:")` to capture user input.

3
Define the For Loop

Set the loop range to evaluate the variables, for example, `For i = startPara To (startPara + pageCount - 1)`.

4
Reset Search Variables

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.

5
Find and Insert Page Breaks

Use the Find method to search for the current paragraph number `i`. Once located, apply `Selection.InsertBreak Type:=wdPageBreak` to execute the formatting.

Use InputBox Variables and a Dynamic For Loop in VBA
Resetting the Search Range: If your macro stops finding paragraphs after the first match, verify that you are collapsing the selection range or resetting the Find object parameters at the top of the loop.

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. 1. Download and Install WPS Office: Get the latest version of WPS Office and ensure the VBA component is installed.
  2. 2. Open Your Document: Launch WPS Writer and open the document containing your numbered paragraphs.
  3. 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. 4. Run Your Macro: Paste your dynamic loop script into a module and click 'Run' to apply the formatting changes instantly.
Excellent compatibility with Microsoft Word macro-enabled files (.docm, .dotm).Built-in Visual Basic Editor to write, test, and debug scripts seamlessly.Lightweight, fast-loading software that easily handles heavy automation tasks.
microsoft office alternative - wps office

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.