使用C#或VBA查找MS Word文档中突出显示文本的背景色
下面查找Word文档中突出显示的文本(即使不同位置的不同文本以不同颜色突出显示)。我们如何找出每个突出显示文本的背景色。我正在使用C#但VBA也可以:使用C#或VBA查找MS Word文档中突出显示文本的背景色,c#,vba,office-interop,ms-word,C#,Vba,Office Interop,Ms Word,下面查找Word文档中突出显示的文本(即使不同位置的不同文本以不同颜色突出显示)。我们如何找出每个突出显示文本的背景色。我正在使用C#但VBA也可以: With ActiveDocument.Range.Find .Highlight = True While .Execute Debug.Print .Parent.Text Wend End With 下面的语句将获得MS Word中选定文本的颜色索引 Dim objSelection As Selection Set
With ActiveDocument.Range.Find
.Highlight = True
While .Execute
Debug.Print .Parent.Text
Wend
End With
下面的语句将获得MS Word中选定文本的颜色索引
Dim objSelection As Selection
Set objSelection = Application.Selection
Msgbox objSelection.FormattedText.HighlightColorIndex
返回的值是wdColorIndex枚举之一。这里可以看到枚举中的值列表
更新#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
我需要找到高亮显示文本的颜色(而不是设置它)
objSelection.ParagraphFormat.Shading.BackgroundPatternColor
为所有高亮显示的文本返回9999999,而不管文本高亮显示的颜色是什么。Find
对象没有FormattedText
属性。如何使用find
对象(显示在我的原始帖子的代码中)查找高亮显示文本的背景色。请参阅我的更新#1