假设下面是MS 2007/2010文档中的文本,我用蓝色高亮显示" testA“,用绿色突出显示"testB”:“这是testA和testB”。我希望以编程方式将testA替换为其背景色索引2,并将testB替换为其背景色索引11。
我试过以下几种方法,但有两个问题:
我希望被替换的案文是:“这是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
发布于 2015-05-18 06:53:04
试试这个:
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
https://stackoverflow.com/questions/30295000
复制相似问题