Don't get me started on Microsoft ...
I have a simple document with "Heading 1", "Heading 2" and "Heading 3" styled paragraphs, and within each heading, text in "Body Text" style.
Sadly some of the Body Text material splits across a page, and I'd rather have all the Body text on the same page as its immediate parent "Heading".
Office 2003 hasn't had "Keep With Before" for at least the last 8 years, so I wrote the attached.
Use:
Place the text cursor anywhere in any paragraph of YOUR text style; it doesn't have to be Body Text.
Run the macro TESTKeepWithBeforeSelection.
Method:
I'm using Selection so you can enjoy the blinking lights.
I leave your original selection in place when I'm done.
I use the style of the FIRST paragraph in your selection as my guiding light.
I turn OFF KeepWithNext and turn ON KeepTogether in every paragraph in the document.
Then for each paragraph in your chosen style, I backup one paragraph to the preceding paragraph and turn ON KeepWithNext.
This seems to work for me.
Function KeepWithBeforeSelection(strStyle As String)
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles(strStyle)
With Selection.Find
.Text = "^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
While Selection.Find.Execute
Selection.MoveUp Unit:=wdParagraph, Count:=2
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
With Selection.ParagraphFormat
.KeepWithNext = True
End With
Selection.MoveDown Unit:=wdParagraph, Count:=2
Wend
End Function
Sub TESTKeepWithBeforeSelection()
Dim rng As Range
Set rng = Selection.Range
Dim strStyle As String
strStyle = Selection.Paragraphs(1).Range.Style.NameLocal
Selection.WholeStory
With Selection.ParagraphFormat
.KeepWithNext = False
.KeepTogether = True
End With
Selection.HomeKey Unit:=wdStory
Call KeepWithBeforeSelection(strStyle)
rng.Select
End Sub