![]() |
|
![]() |
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
![]()
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 Thanks! |
#2
|
||||
|
||||
![]()
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 |
#3
|
|||
|
|||
![]()
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! |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |
![]() |
jeffreybrown | Word VBA | 2 | 01-16-2019 03:23 PM |
Unmerging vertically merged cells | kilroy | Word VBA | 5 | 01-12-2018 12:54 PM |
![]() |
rocky2 | Word VBA | 12 | 12-22-2016 03:03 AM |