【发布时间】:2022-06-14 04:38:22
【问题描述】:
我正在尝试选择两个词之间的范围,在找到的范围内找到一个词,最后为该词着色。
在图像中,我想选择“观察”和“支持信息”之间的范围,然后搜索“管理”字样并将它们涂成红色。
使用我的代码,我可以突出显示第一次出现的单词。
Sub RevisedFindIt4()
' Purpose: highlight the text between (but not including)
' the words "Observation:" and "Supporting Information:" if they both appear.
Dim rng1 As Range
Dim rng2 As Range
Dim rngFound As Range
On Error Resume Next
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set rng1 = ActiveDocument.Range
If rng1.Find.Execute(FindText:="Observation:") Then
Set rng2 = ActiveDocument.Range(rng1.End, ActiveDocument.Range.End)
If rng2.Find.Execute(FindText:="Supporting Information:") Then
Set rngFound = ActiveDocument.Range(rng1.End, rng2.Start)
If rngFound.Find.Execute(FindText:="Management") Then
rngFound.Select
Selection.Range.HighlightColorIndex = wdRed
End If
End If
End If
Selection.HomeKey wdStory
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
【问题讨论】:
-
提示:查找和替换可以在替换过程中突出显示匹配项。录制宏以提供所需的语法,然后编辑代码以使用
rngFound而不是Selection。