none
Why does AllowBreakAcrossPages seem to have no effect? RRS feed

  • Question

  • The below code has no effect except in table properties the allow break across pages is unchecked.
    The tables still break in the middle and span across pages.

    Note: the tables are smaller than 1 page.

    Short of inserting page breaks manually
    Oh What to do?
    Oh What to do?

    tks

    Sub KeepEachTableOnOnePage()
    Dim oTable As Table
    Dim i As Long
    Dim nTables As Long
    Dim nNum As Long

    nTables = ActiveDocument.Tables.Count
    If nTables = 0 Then Exit Sub
    For nNum = 1 To nTables
    Application.StatusBar = nTables & ":" & nNum
    Set oTable = ActiveDocument.Tables(nNum)
    oTable.Rows.AllowBreakAcrossPages = False
    Next nNum
    End Sub

    Saturday, August 25, 2012 6:11 PM

Answers

  • Your macro does not prevent a table breaking across page boundaries - it only prevents individual rows being broken across page boundaries. If you need to keep the whole table on a single page, format all the paragraphs except the last on each row with the 'keep with next' attribute. to do that, you might use code like:

    Dim Tbl As Table, lRow As Long, Rng As Range
    With Tbl
      lRow = .Rows.Count
      Set Rng = ActiveDocument.Range(.Rows(1).Range.Start, .Rows(lRow - 1).Range.End)
      .Rows.AllowBreakAcrossPages = False
      .Rows.First.HeadingFormat = True
      With .Range.ParagraphFormat
        .SpaceBeforeAuto = False
        .SpaceAfterAuto = False
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .WordWrap = True
      End With
      With Rng.ParagraphFormat
        .KeepWithNext = True
        .KeepTogether = True
      End With
    End With
    Set Rng = Nothing: Set Tbl = Nothing

    The line ' .Rows.First.HeadingFormat = True' ensures that, if the table is too long to fit on a whole page, the heading row will appear on the subsequent pages also.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, August 26, 2012 5:26 AM
  • Ok

    Here is the solution that worked for my particular set of different table layouts. And it may be a general case solution.

    The solution

    1. works by rows if the table has no vertically merged cells
    2. and works by cells for those that do.
    3. and use a try catch block to handle the error

    The solution could have worked on cells alone but for lots of tables it is a A register pig so thus to speed it up I implemented two approaches.

            Dim oTable As Word.Table
            Dim nTables As Long
            Dim nNum As Long
    
            nTables = oDoc.Tables.Count
            If nTables = 0 Then Exit Function
            For nNum = 1 To nTables
                oTable = oDoc.Tables(nNum)
    
                Dim lRow As Long, Rng As Word.Range
                With oTable
                    lRow = .Rows.Count
                    .Rows.AllowBreakAcrossPages = False
    
                    With .Range.ParagraphFormat
                        .SpaceBeforeAuto = False
                        .SpaceAfterAuto = False
                        .WidowControl = True
                        .KeepWithNext = False
                        .KeepTogether = False
                        .PageBreakBefore = False
                        .WordWrap = True
                    End With
    
                   ' no work vertically merged so just proceed to proceed by cells
                    Try
                        .Rows.First.HeadingFormat = True
                        Rng = oDoc.Range(.Rows(1).Range.Start, .Rows(lRow - 1).Range.End)
                        With Rng.ParagraphFormat
                            .KeepWithNext = True
                            .KeepTogether = True
                        End With
    
                    Catch
                        Dim oCell As Word.Cell, lCell As Long
                        lCell = .Range.Cells.Count
                        If lCell > 1 Then
                            For Each oCell In oTable.Range.Cells
                                Rng = oDoc.Range(.Range.Cells(1).Range.Start, .Range.Cells(lCell - 1).Range.End)
                                With Rng.ParagraphFormat
                                    .KeepWithNext = True
                                    .KeepTogether = True
                                End With
                            Next oCell
                        End If
                    End Try
                End With
            Next nNum
    
    

    Moving on to the next thing on my list

    Thanks again.

    Seepeg

    Sunday, August 26, 2012 11:42 PM

All replies

  • Your macro does not prevent a table breaking across page boundaries - it only prevents individual rows being broken across page boundaries. If you need to keep the whole table on a single page, format all the paragraphs except the last on each row with the 'keep with next' attribute. to do that, you might use code like:

    Dim Tbl As Table, lRow As Long, Rng As Range
    With Tbl
      lRow = .Rows.Count
      Set Rng = ActiveDocument.Range(.Rows(1).Range.Start, .Rows(lRow - 1).Range.End)
      .Rows.AllowBreakAcrossPages = False
      .Rows.First.HeadingFormat = True
      With .Range.ParagraphFormat
        .SpaceBeforeAuto = False
        .SpaceAfterAuto = False
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .WordWrap = True
      End With
      With Rng.ParagraphFormat
        .KeepWithNext = True
        .KeepTogether = True
      End With
    End With
    Set Rng = Nothing: Set Tbl = Nothing

    The line ' .Rows.First.HeadingFormat = True' ensures that, if the table is too long to fit on a whole page, the heading row will appear on the subsequent pages also.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, August 26, 2012 5:26 AM
  • Hi seepq

    Paul's approach is one possibility; here's another for Word 2003 and later. In that version, it became possible to position a table on the page with "text wrap formatting" - like a graphic. It's what happens when the cursor is in a table and you drag the little symbol that appears at the top left to position the table (and it can also be set in the Table Properties).

    In that original version, when this formatting was active, the table was restricted to a single page. People didn't like that, so in the next version the default became to allow the table to break to the next page. But it's still possible to go back to the old behavior for the entire document. The code:

      oTable.Rows.WrapAroundText = True
      oTable.Parent.Compatibility(wdDontBreakWrappedTables) = True


    Cindy Meister, VSTO/Word MVP

    Sunday, August 26, 2012 7:34 AM
    Moderator
  • The code:

      oTable.Rows.WrapAroundText = True
      oTable.Parent.Compatibility(wdDontBreakWrappedTables) = True


    Cindy Meister, VSTO/Word MVP

    Cindy thanks

    But that old stuff didn't handle tables larger than one page. They did not break at all. And it had other unsatisfactory side effects.

    more feedback in next post

    Sunday, August 26, 2012 11:24 PM
  • Your macro does not prevent a table breaking across page boundaries - it only prevents individual rows being broken across page boundaries.

      With Rng.ParagraphFormat
        .KeepWithNext = True
        .KeepTogether = True
      End With


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thanks Paul

    Your input got me closer. Some of the tables did not break.

    But the snippit of code above causes an error for it seems that if there are any vertically merged cells in a table, you can't access any of the Row objects in that table.

    more feedback in next post

    Sunday, August 26, 2012 11:31 PM
  • Ok

    Here is the solution that worked for my particular set of different table layouts. And it may be a general case solution.

    The solution

    1. works by rows if the table has no vertically merged cells
    2. and works by cells for those that do.
    3. and use a try catch block to handle the error

    The solution could have worked on cells alone but for lots of tables it is a A register pig so thus to speed it up I implemented two approaches.

            Dim oTable As Word.Table
            Dim nTables As Long
            Dim nNum As Long
    
            nTables = oDoc.Tables.Count
            If nTables = 0 Then Exit Function
            For nNum = 1 To nTables
                oTable = oDoc.Tables(nNum)
    
                Dim lRow As Long, Rng As Word.Range
                With oTable
                    lRow = .Rows.Count
                    .Rows.AllowBreakAcrossPages = False
    
                    With .Range.ParagraphFormat
                        .SpaceBeforeAuto = False
                        .SpaceAfterAuto = False
                        .WidowControl = True
                        .KeepWithNext = False
                        .KeepTogether = False
                        .PageBreakBefore = False
                        .WordWrap = True
                    End With
    
                   ' no work vertically merged so just proceed to proceed by cells
                    Try
                        .Rows.First.HeadingFormat = True
                        Rng = oDoc.Range(.Rows(1).Range.Start, .Rows(lRow - 1).Range.End)
                        With Rng.ParagraphFormat
                            .KeepWithNext = True
                            .KeepTogether = True
                        End With
    
                    Catch
                        Dim oCell As Word.Cell, lCell As Long
                        lCell = .Range.Cells.Count
                        If lCell > 1 Then
                            For Each oCell In oTable.Range.Cells
                                Rng = oDoc.Range(.Range.Cells(1).Range.Start, .Range.Cells(lCell - 1).Range.End)
                                With Rng.ParagraphFormat
                                    .KeepWithNext = True
                                    .KeepTogether = True
                                End With
                            Next oCell
                        End If
                    End Try
                End With
            Next nNum
    
    

    Moving on to the next thing on my list

    Thanks again.

    Seepeg

    Sunday, August 26, 2012 11:42 PM
  • <<But that old stuff didn't handle tables larger than one page. They did not break at all. And it had other unsatisfactory side effects.>>

    Mmm, I wouldn't have suggested it, except that I understood that your tables would not be longer than a page and that you didn't want them to break at all.


    Cindy Meister, VSTO/Word MVP

    Monday, August 27, 2012 12:10 PM
    Moderator
  • SOK

    all information which is true is invaluable
    Just because my use of the information was not beneficial to me under my specific circumstances does not make it unhelpful.
    When system engineering it is best to turn over all the rocks to see what is there and how it might be used

    To repeat keywords for future searches
      table breaking across page boundaries
      page break in middle of table
      table text wrap formatting
      Table Rows WrapAroundText
      Table Parent Compatibility Word WdCompatibility wdDontBreakWrappedTables
        KeepWithNext
        KeepTogethe


      VBA Word 2003 word 2008 and later revised behavior

    Keep the true information flowing

    thanks

    I would find it extremely helpful if you could point me in the direction of a cheat sheet of constants
    When you said "oTable.Parent.Compatibility(wdDontBreakWrappedTables) = True"
    well wdDontBreakWrappedTables has compile error for me - "not a member of or is not defined"
    so I search the Word. namespace and deduced that
    oTable.Parent.Compatibility(Word.WdCompatibility.wdDontBreakWrappedTables) = True
    was what was needed and so the code compiles and runs without error

    I need a searchable cheat sheet showing constants are members of?
    ie. wdDontBreakWrappedTables is a member of WdCompatibility is a member of Word

    Monday, August 27, 2012 1:29 PM
  • <<I would find it extremely helpful if you could point me in the direction of a cheat sheet of constants>>

    Ah, sorry, I hadn't clicked that you're using VB.NET, not VBA.

    The best resource I know for deriving the full qualification of a constant is the VBA Editor's "Object Browser". Start any Office app, press Alt+F11 then F2. Type the term in the "Find" box and press Enter.

    With any luck, that will take you directly to the Enum definition. At the very bottom of the OB there's a gray area with further information about the selection, including the full namespace qualification.


    Cindy Meister, VSTO/Word MVP

    Tuesday, August 28, 2012 6:48 AM
    Moderator