Keep with Next if Colon at End of Paragraph

You may want any paragraph that ends with a colon to always be on the same page as the next paragraph. You can handle this by manually applying Keep with next to all such paragraphs. However, there is an easier way. Via this article, you can access a macro that does the work for you – it applies Keep with next to all paragraphs in the active document that end with a colon.

About the Keep with next option

In the Paragraph dialog box, on the Line and Page Breaks tab, you will find some options that can be used to tell Word how to paginate your document.

The Keep with next option in the Paragraph dialog box

If you turn on the Keep with next option for a paragraph, Word will force that paragraph to always follow the next paragraph – the two paragraphs will be on the same page. This is normally what you want to happen for a heading and the following paragraph. In general, a heading should not be the last paragraph on a page – it should always be followed by at least one paragraph. Thus, if the paragraph after a heading moves to the next page, you want the heading to automatically follow. You can manage this easily by making sure that all heading styles have been defined with Keep with next turned on.

However, there are other situations where Keep with next is relevant and where the style in use should not have Keep with next turned on in general. For example, this may be the case for paragraphs that end with a colon.

Macro solution

Via the link below, you can view or download a macro that iterates through all paragraphs in the active document. If a colon is found immediately before the paragraph mark or, in case of a table, before the end of cell marker, the Keep with next option is turned on for that paragraph. A message will appear when finished, telling how many colons were found.

The macro

Below, you will find the macro code. If you need help on installing macros, see How to install a macro.

Option Explicit

Sub ColonParaEnd_SetKeepWithNext()

    '=========================
    'Macro created 2010 by Lene Fredborg, DocTools - www.thedoctools.com
    'THIS MACRO IS COPYRIGHT. YOU ARE WELCOME TO USE THE MACRO BUT YOU MUST KEEP THE LINE ABOVE.
    'YOU ARE NOT ALLOWED TO PUBLISH THE MACRO AS YOUR OWN, IN WHOLE OR IN PART.    
    '=========================
    'The macro iterates through all paragraphs in the active document
    'If a paragraph ends with a colon, Keep with next is applied
    '=========================

    Dim oPara As Paragraph
    Dim n As Long 'used as counter
    
    n = 0
    
    'Iterate though all paragraphs
    For Each oPara In ActiveDocument.Paragraphs
        With oPara
            'If a colon is found immediately before the paragraph mark, Chr(13), apply Keep with next
            'In case of end of a table cell, the end of cell marker is Chr(13) & Chr(7) - alså handle this
            If Right(.Range.Text, 2) = ":" & Chr(13) Or Right(.Range.Text, 3) = ":" & Chr(13) & Chr(7) Then
                .Range.ParagraphFormat.KeepWithNext = True
                n = n + 1
            End If
        End With
    Next oPara
    
    MsgBox "Finished." & vbCr & "Keep with next applied to " & n & " paragraph(s).", vbOKOnly, "Apply Keep With next"
End Sub

Related information

See About VBA Macros and Code Snippets and How to Install a Macro for misc. information that may help you in your work with macros and for information about how to install macros.