【发布时间】:2019-06-05 03:13:02
【问题描述】:
我的 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变量值,但文本颜色不会改变。
【问题讨论】: