Document Editing Problems

How to Insert Page Breaks Before Specific Text Using Word VBA Macro

Algirdas JasaitisAlgirdas Jasaitis Nov 2, 2026 869 views

Question details

The user needs to automate the insertion of page breaks throughout a long document whenever a specific term or text string appears.

How to Insert Page Breaks Before Specific Text Using Word VBA Macro
Product
Microsoft Word
Device & OS
not provided
Scenario
Formatting a long Word document that requires starting a new page every time a specific word or phrase occurs.
Observed behavior
Manually inserting page breaks before repeated terms is time-consuming, so a VBA macro is needed to locate all matching occurrences and apply the page breaks automatically.
Before you start

Before running the macro, ensure that the Developer tab is enabled in your word processor's ribbon interface so you can access the VBA Editor, and verify the exact spelling and casing of the term you want to target.

Solution 1Recommended

Use a VBA Macro with the PageBreakBefore Property

This method modifies the paragraph containing the matched text so it automatically starts on a new page. It is highly recommended because it keeps the document clean without adding invisible manual break characters.

Applying the 'Page break before' paragraph formatting via VBA is much safer for document layouts than inserting hard break characters, as it dynamically forces the targeted text to the top of the next page.

1
Open the VBA Editor

Open your Word document and press 'ALT + F11' on your keyboard to launch the Microsoft Visual Basic for Applications window.

2
Insert a New Module

In the top menu, click 'Insert' and select 'Module' to create a blank workspace for your macro.

3
Paste the Macro Code

Copy and paste the following code into the module window: Sub InsertPageBreaks() Dim r As Range Set r = ActiveDocument.Range With r.Find .Text = "Your term here" Do While .Execute r.Paragraphs(1).PageBreakBefore = True Loop End With End Sub

4
Customize and Run

Change 'Your term here' in the code to the exact phrase you want to trigger the page break. Finally, press 'F5' or click the green 'Run' button in the toolbar to execute the macro.

Use a VBA Macro with the PageBreakBefore Property
Reverting Changes: If you need to undo this action, you can easily press 'Ctrl + Z' immediately after running the macro, or highlight the text, open Paragraph Settings, and uncheck 'Page break before'.
Advanced Document Formatting

Automate Document Formatting Easily with WPS Writer

WPS Writer provides comprehensive support for VBA macros, allowing you to run custom scripts that automate bulk formatting tasks like inserting page breaks, matching exactly how it works in Microsoft Word.

  1. 1. Download and Open WPS Office: Download WPS Office and open your target document in WPS Writer.
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click on 'Macros', or simply press ALT + F11 to launch the VBA editor.
  3. 3. Insert Your Macro: Go to Insert > Module, paste your customized page break macro code, and ensure your target text is correctly specified.
  4. 4. Run the Script: Click 'Run' to apply the automated page breaks across your entire document instantly.
Fully compatible with Microsoft Word (.docx) formats and VBA macros.Lightweight installation that runs smoothly even on older devices.Free to use with a highly intuitive, tabbed user interface.Built-in advanced document formatting and layout management tools.
microsoft office alternative - wps office

Frequently Asked Questions

How do I undo the page breaks inserted by the VBA macro?

If you used the 'PageBreakBefore' method, select the affected text, right-click and choose 'Paragraph', navigate to the 'Line and Page Breaks' tab, and uncheck 'Page break before'. If you used the manual 'InsertBreak' method, press 'Ctrl + H' to open Find and Replace, type '^m' (the code for a manual page break) in the 'Find what' box, leave 'Replace with' empty, and click 'Replace All'.

Why is my VBA macro not running or giving a security warning?

Your macro security settings might be blocking execution. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings, and select 'Disable all macros with notification'. When you open the document, click 'Enable Content' on the warning bar. Also, make sure your file is saved as a Macro-Enabled Document (.docm).

Can I modify the macro to insert a section break instead of a standard page break?

Yes. If you are using the manual InsertBreak method script, you can change the line 'oRngBreak.InsertBreak' to 'oRngBreak.InsertBreak Type:=wdSectionBreakNextPage'. This will force the macro to insert a Next Page section break, which is useful if you need to change margins or orientation on the new pages.