假设MS Word 2007/2010文档中的以下文本,我用蓝色突出显示了“ testA”,用绿色突出显示了“ testB”:“这是testA和testB。”。我想以编程方式将testA替换为其背景颜色索引2,并将testB替换为其背景颜色索引11。参考:WdColorIndex枚举
我尝试了以下操作,但有两个问题:
我希望替换的文本为:“这是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
试试这个:
Sub ReplaceHighlightedTextColor()
Dim rng As Range
Set rng = Selection.Range
With rng.Find
.ClearFormatting
.Highlight = True
While .Execute(Forward:=True, Format:=True)
'Note: 'rng' is now the range containing the matched content
rng.Text = rng.FormattedText.HighlightColorIndex
Wend
End With
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句