【问题标题】:Adjust Table Heading Color With Merged Cells使用合并的单元格调整表格标题颜色
【发布时间】:2020-07-05 02:57:14
【问题描述】:

我有大量的报告,其中包含多个需要调整标题颜色的表格。不过,我遇到了在标题中垂直合并单元格的表格的问题。代码如下:

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

此操作失败并显示以下错误消息:

运行时错误“5991”:

无法访问此集合中的各个行,因为表格有垂直合并的单元格

我尝试通过表格中的每个单元格调整为循环,但发现 word 中的表格没有.cells 属性,因此For each cell in table.cells 样式循环不起作用。

还有其他方法可以做到这一点吗?可能直接编辑表格样式?

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    Table 对象没有 Cells 属性,但 Table.Range 对象有,所以:

    For Each cel in Table.Range.Cells
    

    应该有效:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-02
      • 2015-02-03
      • 2014-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      相关资源
      最近更新 更多