我有一些Word文档,其中的表格具有颜色编码的单元格,在这些单元格中,我希望通过更改单元格的颜色来“隐藏”每个单元格中的某些单词。
我有一个VBA脚本,它从数组列表中搜索单词并成功地找到它们,但我无法让这些单词将颜色更改为单元格的背景颜色。
Sub TableWordColorReplace()
Dim C As Cell
Dim tableCount As Long
Dim Ctr As Integer
Dim backColor As Long
Dim i As Long
Dim range As range
Dim TargetList
tableCount = ActiveDocument.Tables.Count 'to account for any/all tables in the document
TargetList = Array("word1", "word2", "word3")
For Ctr = 1 To tableCount 'cycle thru each table in the document
For Each C In ActiveDocument.Tables(Ctr).range.Cells 'search in every cell in the table
backColor = C.Shading.BackgroundPatternColor 'the color I want to change the found text to
For i = 0 To UBound(TargetList) 'cycle thru each word in the list
With C.range.Find
.Text = TargetList(i)
With .Replacement
.Text = TargetList(i)
.ClearFormatting
.Font.Color = backColor 'this is where I expect the word color to change, but it doesn't
End With
.Execute Replace:=wdReplaceAll
End With
Next
Next C
Next Ctr
End Sub
我的期望是,当在任何单元格中找到单词时,代码的.Replacement部分会将单词颜色更改为backColor变量值,但文本颜色保持不变。
https://stackoverflow.com/questions/56449297
复制相似问题