Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-06-2024, 09:52 AM
amankap amankap is offline Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells Windows 10 Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells Office 2021
Novice
Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells
 
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
  #2  
Old 06-06-2024, 04:57 PM
Guessed's Avatar
Guessed Guessed is offline Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells Windows 10 Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

That setting is inherited from the table style so it could be as simple as changing the table style setting if your document is extraordinarily well formatted. However, typically tables have a large number of local formatting applied to them so the style setting is likely to be overridden in various locations so a more direct method will need to be used.

You can apply the AllowBreakAcrossPages setting without having to go to every single row and cell in the table. This avoids the need to worry about tables with merged cells
Code:
Sub TablePageBreaker()
  Dim aTbl As Table
  For Each aTbl In ActiveDocument.Tables
    aTbl.Rows.AllowBreakAcrossPages = False
  Next aTbl
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 06-07-2024, 01:30 AM
amankap amankap is offline Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells Windows 10 Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells Office 2021
Novice
Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells
 
Join Date: May 2024
Posts: 8
amankap is on a distinguished road
Default

Thanks Andrew. I like your approach of changing the property at table level.


However, my main issue is still there. For rows corresponding to the vertically merged cells, how to programmatically enable paragraph property: 'Keep with next'?


Regards!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells Deleting a table with vertically merged cells if it contains certain text. bpike Word VBA 4 10-30-2020 10:28 PM
Finding Tables with Vertically Merged Cells T-Belle Word VBA 10 07-19-2020 07:09 PM
Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells Format tables with vertically merged cells jeffreybrown Word VBA 2 01-16-2019 03:23 PM
Unmerging vertically merged cells kilroy Word VBA 5 01-12-2018 12:54 PM
Macro to Disable 'Allow Row to Break Across Pages' in Tables with Vertically Merged Cells Overcome issues in tables with vertically merged cells rocky2 Word VBA 12 12-22-2016 03:03 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:10 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft