首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用VBA改变word文档表字符串中单个单词的颜色以匹配单元格颜色

用VBA改变word文档表字符串中单个单词的颜色以匹配单元格颜色
EN

Stack Overflow用户
提问于 2019-06-05 02:14:51
回答 1查看 529关注 0票数 0

我有一些Word文档,其中的表格具有颜色编码的单元格,在这些单元格中,我希望通过更改单元格的颜色来“隐藏”每个单元格中的某些单词。

我有一个VBA脚本,它从数组列表中搜索单词并成功地找到它们,但我无法让这些单词将颜色更改为单元格的背景颜色。

代码语言:javascript
复制
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变量值,但文本颜色保持不变。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56449297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档