How to Apply Keep With Next Between Heading Levels Using Word Macros
Question details
The user wants to format a long document so that specific heading levels (e.g., Heading 4 and 5) stay together on the same page, potentially using a macro for conditional formatting.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Formatting heading levels in a long document to prevent unwanted page breaks between consecutive, related headings.
- Observed behavior
- The goal state is to ensure that a heading is only kept with the next paragraph if that paragraph is of a specific subordinate heading style, avoiding unnecessary grouping of regular text paragraphs.
Before modifying base heading styles or running a macro, create a backup copy of your long document to easily revert any unintended formatting changes.
Modify Heading Styles for Keep With Next
Configure the built-in heading styles to universally apply the 'Keep with next' property. This is often simpler than using a macro and resolves most pagination issues.
By modifying the style itself rather than individual paragraphs, Word will automatically keep your headings attached to the following paragraph. Ensure that your standard body text style does not have this setting enabled, otherwise, large blocks of text will jump to the next page.
Navigate to the Home tab and click the small arrow icon in the bottom right corner of the Styles group to open the Styles pane.
Right-click the specific heading style you want to adjust (for example, Heading 4) and select 'Modify' from the context menu.
In the Modify Style dialog box, click the 'Format' button in the bottom left corner and select 'Paragraph'.
Switch to the 'Line and Page Breaks' tab, check the box for 'Keep with next', and click OK to apply the changes to all instances of the style.

Run a VBA Macro for Conditional Formatting
Use a VBA macro to inspect each paragraph and apply 'Keep with next' strictly when the following paragraph matches a specific heading level requirement.
Manage Advanced Heading Styles with WPS Writer
WPS Writer provides comprehensive styling tools and VBA macro support, making it simple to perfectly format long documents and control paragraph pagination.
- 1. Open Your Document: Launch WPS Writer and open the long document you wish to format.
- 2. Access Style Modifications: Go to the Home tab, find your target Heading style in the ribbon, right-click it, and select 'Modify Style'.
- 3. Configure Line and Page Breaks: Click 'Format' and then 'Paragraph'. Under the 'Line and Page Breaks' tab, check 'Keep with next' to prevent the heading from separating from the following text.
- 4. Run Macros (Optional): If you require conditional logic, navigate to the Developer tab to open the VBA editor and execute your custom formatting scripts.

Frequently Asked Questions
What does 'Keep with next' actually do in a document?
The 'Keep with next' setting is a paragraph formatting rule that binds a selected paragraph to the one immediately following it, ensuring that a page break never occurs between the two. This is heavily used for headings so they don't appear isolated at the bottom of a page.
Why are large sections of my text moving to the next page unexpectedly?
This usually happens when 'Keep with next' or 'Keep lines together' is accidentally applied to your standard body text (like the Normal style). When consecutive paragraphs have this setting enabled, the word processor attempts to keep them all on the same page, causing massive page jumps.
Can I use VBA macros to format documents in WPS Office?
Yes, WPS Office offers VBA support in its professional versions. You can open the macro editor from the Developer tab to write, paste, and run VBA scripts for complex conditional document formatting just like in Microsoft Word.




