在MS Word中的VBA RegEx搜索中包含所有故事



我正在寻找一种能够在MS Word中执行RegEx搜索的解决方案。我一直在使用以下代码来实现这一点(它突出显示匹配的字符串;然后我可以使用Word的内置搜索功能搜索突出显示的实例(,但它仅限于主文档。出于我的目的,我能够在搜索中包括任何其他故事,尤其是任何尾注和/或脚注,这一点至关重要。

有人能解决在此类搜索中包含尾注和脚注故事的问题吗?

Sub RegexHighlight()
Set RegExp = CreateObject("VBScript.RegExp")
With RegExp
.Global = True
.Pattern = InputBox("Find what:")
For Each Match In RegExp.Execute(ActiveDocument.Range.Text)
ActiveDocument.Range(Match.FirstIndex, Match.FirstIndex + Match.Length) _
.HighlightColorIndex = wdYellow
Next
End With
End Sub

下面是一个搜索所有故事的例子

Sub RegexHighlight()
Dim Match As Object
Dim Pattern As String
Dim Story As Range
Pattern = InputBox("Find what:")
If Pattern = "" Then Exit Sub
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = Pattern
For Each Story In ActiveDocument.storyRanges
Do While Not Story Is Nothing
For Each Match In .Execute(Story)
Story.SetRange Match.FirstIndex, Match.FirstIndex + Match.Length
Story.HighlightColorIndex = wdYellow
Next
Set Story = Story.NextStoryRange
Loop
Next
End With

End Sub

最新更新