以下在Word文档中查找突出显示的文本(即使不同位置的不同文本以不同颜色突出显示)。我们如何找出每个突出显示文本的背景颜色。我使用C#但VBA也没问题:
With ActiveDocument.Range.Find
.Highlight = True
While .Execute
Debug.Print .Parent.Text
Wend
End With
答案 0 :(得分:0)
以下语句将为您提供MS Word中所选文本的颜色索引
Dim objSelection As Selection
Set objSelection = Application.Selection
Msgbox objSelection.FormattedText.HighlightColorIndex
返回的值是wdColorIndex枚举之一。枚举中的值列表可以在https://msdn.microsoft.com/en-us/library/bb237561(v=office.12).aspx
中看到<强>更新#1 强>
下面的代码是按要求使用ActiveDocument.Range.Find
Dim objSelection As Selection
Dim temp As String
With ActiveDocument.Range.Find
.Highlight = True
While .Execute
'Store the text for searching later
temp = .Parent.Text
'Select all the text
ActiveDocument.Content.Select
'Find and select the text
With Selection.Find
.Text = temp
End With
If Selection.Find.Execute Then
Selection.Select
End If
'Now that we've selected it, get the HighlightedColorIndex
Set objSelection = .Application.Selection
MsgBox .Parent.Text & vbNewLine & "Index: " & objSelection.FormattedText.HighlightColorIndex
Wend
End With