How Do I Clear the KeepWithNext Flag on Every Pragraph in a Table Except For the One in Table Header?

Howdy,

I am trying to clear the KeepWithNext flag for all the paragraphs in every table in the document but the ones in table headings.

What's the best way to do that.

At the moment I only have the following script that clears the KeepWithNext flag for every paragraph in every table.

Sub ClearKeepWNext()
Dim t As Table
   For Each t In ActiveDocument.Tables
        t.Range.ParagraphFormat.KeepWithNext = False
        t.AutoFitBehavior (wdAutoFitContent)
   Next
End Sub

This results in table headers split so that many tables have only the table header at the bottom of the page where all other table rows start on the text page.

What would be the best way to fix that?

Thank you in ad

February 19th, 2015 10:31am

Try:

Sub ClearKeepWNext()
Dim Tbl As Table, i As Long
For Each Tbl In ActiveDocument.Tables
  With Tbl
    .Range.ParagraphFormat.KeepWithNext = False
    For i = 1 To .Rows.Count
      With .Rows(i)
        If .HeadingFormat = True Then
          .Range.ParagraphFormat.KeepWithNext = True
        Else
          Exit For
        End If
      End With
    Next
  End With
Next
End Sub

If the tables only have a single heading row, you could instead use:

Sub ClearKeepWNext()
Dim Tbl As Table
For Each Tbl In ActiveDocument.Tables
  With Tbl
    .Range.ParagraphFormat.KeepWithNext = False
    With .Rows(1)
      If .HeadingFormat = True Then
        .AllowBreakAcrossPages = False
      End If
    End With
  End With
Next
End Sub

Free Windows Admin Tool Kit Click here and download it now
February 19th, 2015 11:09pm

Thanks! Any possibility to handle error 5991? Looks like merged rows in some table cause this error so that currently this script stops somewhere in the middle of the document.

I believe, I have to insert ErrorCatcher with something as

If Error.Number = 5991 Then Resume

Where do I place this?

Sub ClearKeepWNext()
 Dim Tbl As Table, i As Long
 For Each Tbl In ActiveDocument.Tables
   With Tbl
     .Range.ParagraphFormat.KeepWithNext = False
     For i = 1 To .Rows.Count
       With .Rows(i)
         If .HeadingFormat = True Then
           .Range.ParagraphFormat.KeepWithNext = True
         Else
           Exit For
         End If
       End With
     Next
   End With
ErrCatcher:
If Err.Number = 5991 Then
 Resume Next
End If
 End Sub

Does not work...

February 20th, 2015 12:29pm

Are you marking your table heading rows as such in the Table controls?
Free Windows Admin Tool Kit Click here and download it now
February 21st, 2015 3:25pm

Are you marking your table heading rows as such in the Table
February 25th, 2015 4:57am

Figured this myself. In order to skip tables with vertically merged cells, I do the following:

Sub ClearKeepWNext()
 Dim Tbl As Table, i As Long
 On Error GoTo ErrHandler
 For Each Tbl In ActiveDocument.Tables
   With Tbl
     .Range.ParagraphFormat.KeepWithNext = False
     For i = 1 To .Rows.Count
       With .Rows(i)
         If .HeadingFormat = True Then
           .Range.ParagraphFormat.KeepWithNext = True
         Else
           Exit For
         End If
       End With
     Next
   End With
NextTbl:
    Next
ErrHandler:
    Select Case Err
      Case 5991
         MsgBox "Table #" & Tbl.Range.Cells.Item(5).Range.Text & "has vertically merged cells"
         Resume NextTbl
    End Select
End Sub

Free Windows Admin Tool Kit Click here and download it now
February 25th, 2015 11:32am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics