Word 文档高亮显示问题

huangapple go评论56阅读模式
英文:

Word document highlight issues

问题

抱歉,我不能提供代码的翻译。

英文:

Hi I am very new to VBA word programming. I am trying to use regex and word range to select a pattern of words in a word document containing 108 pages to highlight them in Yellow and Green. When I execute the VBA code the word document hangs for a minute of 2 until the code has processed the request. Please check the code below and suggest.

**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

答案1

得分: 1

尝试这个。

子 高亮特定词()
    
    Dim sArr() As String
    Dim rTmp As Range
    Dim x As Long
    sArr = Split("highlight specific words") ' your list
    Options.DefaultHighlightColorIndex = wdYellow
    
    For x = 0 To UBound(sArr)
        Set rTmp = ActiveDocument.Range
        With rTmp.Find
            .Text = sArr(x)
            .Replacement.Text = sArr(x)
            .Replacement.Highlight = True
            .Execute Replace:=wdReplaceAll
        End With
    Next
    
End Sub
英文:

Try this.

Sub HighlightSpecificWords()

    Dim sArr() As String
    Dim rTmp As Range
    Dim x As Long
    sArr = Split("highlight specific words") ' your list
    Options.DefaultHighlightColorIndex = wdYellow

    For x = 0 To UBound(sArr)
  Set rTmp = ActiveDocument.Range
  With rTmp.Find
            .Text = sArr(x)
            .Replacement.Text = sArr(x)
            .Replacement.Highlight = True
            .Execute Replace:=wdReplaceAll
End With
    Next

End Sub

Before:

Word 文档高亮显示问题

After:

Word 文档高亮显示问题

huangapple
  • 本文由 发表于 2023年2月19日 07:13:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定