![](/img/trans.png)
[英]Find out background color of highlighted text in MS Word document using C# or VBA
[英]Using MS Word VBA how to Find and Replace highlighted text with the value of the highlight color
假設MS Word 2007/2010文檔中的以下文本,我用藍色突出顯示“testA”,用綠色突出顯示“testB”:“這是testA和testB。”。 我想以編程方式將testA替換為其背景顏色索引2,並將testB替換為其背景顏色索引11. Ref: 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.