嗨,我是 VBA 文字编程的新手。我正在尝试使用正则表达式和单词范围来选择包含 108 页的 word 文档中的单词模式,以用黄色和绿色突出显示它们。当我执行 VBA 代码时,word 文档会挂起 2 分钟,直到代码处理完请求。请检查下面的代码并提出建议。
**extract of the word**
*QR233A(M/W)
if LRD233 xs , LRD237 xs
LRDE233 xs , LRDE237 xs
then @R233A(M/W)
\
.
*QZR233A(M/W) @R233A(M/W) .
*QAR233A(M/W)
if LRD233 xs , LRD237 xs
LRDE233 xs , LRDE237 xs
LARSUDKFTHJS s , LARSUDKFLMS s
then @R233A(M/W)
\
.
*R233A(M/W)
if R233A(M/W) a
P831 cnf , P833 cnf , L(PB-1)SETTINGAVAIL xs
LSGPBA xs
then R233A(M/W) s
P831 cn , P833 cn
**VBA Code**
Sub Reminder_Highlight()
Dim match As VBScript_RegExp_55.match
Dim matches As VBScript_RegExp_55.MatchCollection
Dim myrange As Range
Dim rng3 As Selection
Dim counter As Integer
Set myrange = ActiveDocument.Content
Set rng3 = Selection
Dim Panel_request As Boolean
Dim Reminder_latch As Boolean
With New VBScript_RegExp_55.RegExp
.Pattern = "(\*Q(A|R|RD)\S+|LRD\S+\s(xs\s+,|xs)|(\@R|\*R)\S+)"
.Global = True
Set matches = .Execute(rng3.Text)
End With
Debug.Print matches.Count
For Each match In matches
myrange.SetRange rng3.Characters(match.FirstIndex + 1).Start, rng3.Characters(match.FirstIndex + match.Length).End
If Left(match, 1) = "@" Or Mid(match, 1, 2) = "*R" Then myrange.HighlightColorIndex = wdBrightGreen Else myrange.HighlightColorIndex = wdYellow
Debug.Print matches.Item(counter) & " "; counter
counter = counter + 1
Next
Set matches = Nothing
Set rng2 = Nothing
Set rng1 = Nothing
Set rng3 = Nothing
End Sub