您可以在 Word 本身中使用通配符搜索来完成此任务,而无需调用 Regexp。下面的代码将返回范围对象的 scripting.dictionary,您可以从中提取文本,或者通过非常小的调整返回捕获的文本。键功能允许您定义要使用的括号集以及要用于引号的字符。在下面的测试用例中,我使用了 word 中的智能引号字符。
我使用的测试文本是
Blah blah blah (blah "Text 1" blah) blah blah blah Blah blah blah (blah “Text 2” blah) Blah blah blah (blah “Text 3” blah) Blah blah blah (blah “Text 4” blah)
输出为
Text 2
Text 3
Text 4
因为第一组引号不是智能引号。您不会说您是否只需要提取文本或查找文本,然后以某种方式在 Word 文档中对其进行处理,因此我的第一个选择是返回找到的文本的 Word.Ranges。获取文本的调整在函数的 cmets 中提供。
下面的代码不会对神奇的 RubberDuck 插件进行任何代码检查。
Public Sub testGetTextInQuotesInBrackets()
Dim myTexts As Scripting.Dictionary
Set myTexts = _
GetTextInQuotesInBrackets _
( _
"(,)", _
ChrW$(&H201C) & "," & ChrW$(&H201D), _
ActiveDocument.StoryRanges(wdMainTextStory) _
)
Dim myItem As Variant
For Each myItem In myTexts
Debug.Print myTexts.Item(myItem).Text
' if just the text was collected
'Debug.Print myItem
Next
End Sub
'@Description("Returns a scripting.Dictionary of long vs word.range objects)
Function GetTextInQuotesInBrackets _
( _
ByVal ipBrackets As String, _
ByVal ipQuotes As String, _
ByRef ipRange As Word.Range _
) As Scripting.Dictionary
Dim myTextRanges As Scripting.Dictionary
Set myTextRanges = New Scripting.Dictionary
Dim myBrackets As Variant
myBrackets = Split(ipBrackets, ",")
Dim myQuotes As Variant
myQuotes = Split(ipQuotes, ",")
With ipRange
With .Find
.ClearFormatting
.Text = "[" & myBrackets(0) & "]*[" & myQuotes(0) & "]*[" & myQuotes(1) & "]" ' is there any need to process the following closing bracket
.MatchWildcards = True
.Wrap = wdFindStop
End With
Do While .Find.Execute
Dim myFoundRange As Word.Range
Set myFoundRange = .Duplicate
With myFoundRange
.MoveStartUntil cset:=myQuotes(0)
' Select the text within the quotes
.MoveStart Count:=1
.MoveEnd Count:=-1
End With
myTextRanges.Add myTextRanges.Count, myFoundRange
' Alternatively, if you just need the text
'myTextRanges.add myTextRanges.count, myFoundRange.Text
.Start = myFoundRange.End + 2
.End = ipRange.End
Loop
End With
Set GetTextInQuotesInBrackets = myTextRanges
End Function