【发布时间】:2015-07-29 11:21:48
【问题描述】:
假设 MS Word 2007/2010 文档中有以下文本,“testA”用蓝色突出显示,“testB”用绿色突出显示:“这是 testA 和 testB。”。我想以编程方式将 testA 替换为其背景颜色索引 2,并将 testB 替换为其背景颜色索引 11。参考:WdColorIndex Enumeration
我尝试了以下方法,但有两个问题:
- 它将 testA 替换为 0(默认背景的颜色索引),将 testB 替换为 2(即 testA 的颜色索引)
- 虽然循环没有结束
我希望替换的文本是:“这是 2 和 11”。相反,我得到:“这是 0 和 2”。
任何使用 VBA 或 C# 的更正都可以。
Sub ReplaceHighlightedTextColor()
With Selection.Find
.ClearFormatting
.Highlight = True
While .Execute(Forward:=True, Format:=True, ReplaceWith:=CStr(Selection.FormattedText.HighlightColorIndex))
Wend
End With
End Sub
【问题讨论】:
标签: vba office-interop ms-word