0
votes

I have a large number of reports which have multiple tables that need to have their heading color adjusted. I'm running into an issue with tables who have vertically merged cells in the header though. Here's the code:

Sub Recolor_Table()
    Dim Doc_Table As Table

    For Each Doc_Table In ActiveDocument.Tables
        If Doc_Table.Style Like "*Non-Results Table*" Then
            'Since this will match "*Results Table*" as well, check it first
            Doc_Table.Rows(1).Shading.BackgroundPatternColor = 12566463   'Grey
        ElseIf Doc_Table.Style Like "*Results Table*" Then
            Doc_Table.Rows(1).Shading.BackgroundPatternColor = 8406272   'Blue
        End If
    Next Doc_Table
End Sub

This fails with the following error message:

Run-time error '5991':

Cannot access individual rows in this collection because the table has vertically merged cells

I tried adjusting to a loop through each cell in the table, but found that tables in word do not have a .cells property, so a For each cell in table.cells style loop wouldn't work.

Is there any other way to do this? Possibly editing the table style directly?

1

1 Answers

1
votes

The Table object doesn't have a Cells property, but the Table.Range object does, so:

For Each cel in Table.Range.Cells

should work:

Sub LoopCellsInTableWithMergedCells()
    Dim tbl As word.Table
    Dim cel As word.Cell

    Set tbl = ActiveDocument.Tables(1)
    For Each cel In tbl.Range.Cells
        Debug.Print cel.rowIndex, cel.ColumnIndex
    Next
End Sub