向后迭代 MatchCollection

Iterate a MatchCollection backwards

如何向后迭代正则表达式的 MatchCollection? 以下代码不起作用 (VBA-MSWORD)

Set mtch = foundmatches(m) 给出 'Invalid procedure call or argument (Error 5)'

Dim regex As New regexp
Dim foundmatches As MatchCollection
Dim mtch As Match

With regex
    .PATTERN = "somepattern"
    .Global = True
    Set foundmatches = .Execute(ActiveDocument.Range)
End With

For m = foundmatches.Count To 1 Step -1
    Set mtch = foundmatches(m) 'Invalid procedure call or argument (Error 5)
    'do stuff
Next m 

谢谢

像这样,因为 Matches 集合是从零开始的

For m = foundmatches.Count -1 To 0 Step -1
    Set mtch = foundmatches(m) 'Invalid procedure call or argument (Error 5)
    'do stuff
Next m