问题描述
我在Word中有一个表是通过重复节内容控件构建的。 CC的“重复”部分的单元格中有文本内容控件。
我能够根据文本给出字体颜色;但除了最后一行,我无法更改单元格的底纹。
在调试会话中,无论我看到正确的行号和列号,Shading.BackgroundPatternColor
都不会改变颜色。令人惊讶的是,它可以在表的最后一行工作。
Dim CC As ContentControl
Dim TableNum As Long,RowNum As Long,ColNum As Long
For Each CC In ActiveDocument.ContentControls
If CC.Tag = "tagPriority" Then
If CC.Range.Text = "Critical" Then
CC.Range.Font.TextColor = wdColorAutomatic
If CC.Range.information(wdWithInTable) Then
TableNum = Me.Range(0,CC.Range.End).Tables.Count
RowNum = CC.Range.information(wdStartOfRangeRowNumber)
ColNum = CC.Range.information(wdStartOfRangeColumnNumber)
ActiveDocument.Tables(TableNum).Cell(RowNum,ColNum).Shading.BackgroundPatternColor = wdColorDarkRed
End If
...
我也从the code in stackoverflow那里得到了帮助
解决方法
更改后
TableNum = Me.Range(0,CC.Range.End).Tables.Count
到
TableNum = ActiveDocument.Range(0,CC.Range.End).Tables.Count
它为我工作。我将您的代码放在一个标准模块中,因此,如果您将代码放在ThisDocument
模块中的文档事件处理程序中,则将它放入YMMV。
当然还有另一种获取表的方法也可以使用
Dim tbl As Table
Set tbl = CC.Range.Tables(1)
tbl.Cell(RowNum,ColNum).Shading.BackgroundPatternColor = wdColorDarkRed
编辑: 我使用的代码:
Sub AddCellShading()
Dim CC As ContentControl
Dim tbl As Table,RowNum As Long,ColNum As Long
For Each CC In ActiveDocument.ContentControls
If CC.Tag = "tagPriority" Then
If CC.Range.Text = "Critical" Then
CC.Range.Font.TextColor = wdColorAutomatic
If CC.Range.Information(wdWithInTable) Then
Set tbl = CC.Range.Tables(1)
RowNum = CC.Range.Information(wdStartOfRangeRowNumber)
ColNum = CC.Range.Information(wdStartOfRangeColumnNumber)
tbl.Cell(RowNum,ColNum).Shading.BackgroundPatternColor = wdColorDarkRed
End If
End If
End If
Next CC
End Sub