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