TJ87
TJ87

Reputation: 542

Insert page break after every table and keep caption with table?

I posted about How do I insert a page break after every table in a document?. Now I have captions above all my tables (Style = Table Caption). When I run the macro from user taller, each caption remains on the page before the break. I would like the captions to stay with the tables.

Upvotes: 0

Views: 66

Answers (1)

taller
taller

Reputation: 18963

  • Use Find method to locate all table caption, then insert a page break
Sub InsertPageBreakAtCaptionEnd()
    Dim docRng As Range, bSkip As Boolean
'    Const STYLE_NAME = "Caption"  ' for testing
    Const STYLE_NAME = "Table Caption"  ' modify as needed
    Set docRng = ActiveDocument.Content
    With docRng.Find
        .ClearFormatting
        .Style = ActiveDocument.Styles(STYLE_NAME)
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        bSkip = True ' flag to skip the first table
        Do While .Execute
            docRng.Collapse Word.wdCollapseStart
            If bSkip Then
                bSkip = False
            Else
                docRng.InsertBreak Type:=wdPageBreak
            End If
            docRng.Move Word.wdParagraph, 1
        Loop
    End With
End Sub

Upvotes: 0

Related Questions