Document Editing Problems

How to Apply Keep With Next Between Heading Levels Using Word Macros

Nimra MalikNimra Malik Nov 2, 2026 869 views

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.

How to Apply Keep With Next Between Heading Levels Using Word Macros
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 you start

Before modifying base heading styles or running a macro, create a backup copy of your long document to easily revert any unintended formatting changes.

Solution 1Recommended

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.

1
Open the Styles Pane

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.

2
Modify the Heading Style

Right-click the specific heading style you want to adjust (for example, Heading 4) and select 'Modify' from the context menu.

3
Adjust Paragraph Settings

In the Modify Style dialog box, click the 'Format' button in the bottom left corner and select 'Paragraph'.

4
Enable Keep With Next

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.

Modify Heading Styles for Keep With Next
Check Normal Style: Verify that your 'Normal' style or body text style does not have 'Keep with next' or 'Keep lines together' enabled to prevent erratic page breaks.
Professional Word Processor

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. 1. Open Your Document: Launch WPS Writer and open the long document you wish to format.
  2. 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. 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. 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.
100% compatible with Microsoft Word document formats and heading stylesBuilt-in support for VBA macros to automate conditional formatting tasksIntuitive paragraph and style management panels for precise layout controlLightweight performance even when editing extremely long documents
microsoft office alternative - wps office

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.