View Single Post
 
Old 05-29-2018, 02:45 PM
jjfreedman jjfreedman is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: May 2012
Location: https://jay-freedman.info
Posts: 39
jjfreedman is on a distinguished road
Default

Word VBA always has refused to deal with rows in a table that contains vertically merged cells, or with columns in a table that contains horizontally merged cells. If your table has only vertically merged cells, you can run the For Each on the columns instead of the rows, like this:
Code:
Sub test()
    Dim tbl As Table
    Dim col As Column
    Dim cel As Cell
    
    For Each tbl In ActiveDocument.Tables
        For Each col In tbl.Columns
            For Each cel In col.Cells
                If cel.Shading.BackgroundPatternColor = RGB(225, 225, 153) Then
                    cel.Shading.BackgroundPatternColor = RGB(225, 225, 225)
                End If
            Next cel
        Next col
    Next tbl
End Sub
Another alternative is to go through all cells in the table's Range, like this:
Code:
Sub test()
    Dim tbl As Table
    Dim cel As Cell
    
    For Each tbl In ActiveDocument.Tables
        For Each cel In tbl.Range.Cells
            If cel.Shading.BackgroundPatternColor = RGB(225, 225, 153) Then
                cel.Shading.BackgroundPatternColor = RGB(225, 225, 225)
            End If
        Next cel
    Next tbl
End Sub
Either way, you could run into this problem: If the RGB(225, 225, 153) shading was applied after the cells were merged, then VBA will tell you that the BackgroundPatternColor of the merged cells is -1 instead of the actual RGB value, and the If statement will see the condition as False. That happens because only one of the cells that were merged together has the shading you expect, and the others are not shaded. The only way to avoid that is to unmerge, shade all of the cells that will be included, and then merge them again.
Reply With Quote