View Single Post
 
Old 06-06-2024, 09:52 AM
amankap amankap is offline Windows 10 Office 2021
Novice
 
Join Date: May 2024
Posts: 8
amankap is on a distinguished road
Default Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells

I have a large Word document with many tables. The requirement is to have a macro that can read all tables and globally disable the row property: 'Allow row to break across pages'. I am able to do this with the code below, provided there are no tables in the document with vertically merged cells.
(Currently, I have implemented some exception handling that generates a list of tables that are not processed.)
Code:
Sub FormatTable1()
    Dim table_instance As Table
    Dim cell_instance As Cell
    Dim table_instance_index As Long
    Dim unprocessed_tables_string As String
    Dim total_table_count As Long
    Dim table_heading As String
    Dim rowBreakDisabled As Boolean
 
 
 
    total_table_count = ActiveDocument.Tables.Count
 
    If total_table_count = 0 Then
        MsgBox "No tables found in the converted document."
        Exit Sub
    End If
 
    For table_instance_index = 1 To total_table_count
        Set table_instance = ActiveDocument.Tables(table_instance_index)
        table_heading = ""
        rowBreakDisabled = True
 
        ' Extract table heading
        If table_instance.Range.Start > 1 Then
            table_heading = ActiveDocument.Range(table_instance.Range.Start - 1, table_instance.Range.Start).Paragraphs(1).Range.Text
            table_heading = Replace(table_heading, vbCr, "")
        End If
 
        ' Disable row breaking
        On Error Resume Next
        For Each cell_instance In table_instance.Range.Cells
            cell_instance.Row.AllowBreakAcrossPages = False
            If Err.Number <> 0 Then
                Err.Clear
                rowBreakDisabled = False
                Exit For
            End If
        Next cell_instance
        On Error GoTo 0
 
        ' Collect table processing info
        If Not rowBreakDisabled Then
            unprocessed_tables_string = unprocessed_tables_string & "Table " & table_instance_index & " (" & table_heading & ")." & vbCrLf
        End If
    Next table_instance_index
 
    MsgBox "Total tables in the Word document: " & total_table_count & vbCrLf & vbCrLf & _
            "Tables not processed as they have vertically merged cells:" & vbCrLf & unprocessed_tables_string
End Sub
Can someone please help me execute this code when the document also contains tables with rows that have vertically merged cells? For rows corresponding to vertically merged cells, can we enable paragraph property: 'Keep with next'? Or any other solution/approach will also be appreciated.


Thanks!
Reply With Quote