将两个单独的查找和格式化宏合并为一个

Combine two separate Find and Format Macros into one

目前,我正在使用两个单独的宏来查找 Word 文档中的特定文本并执行某些格式化命令(每次搜索都不同)。为了方便起见,我想以某种方式将两者合二为一。

这是第一个:

With Selection.Find
.Text = "potatoes"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
'
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.InsertParagraphAfter
Repeat (3)
           

第二个:

With Selection.Find
.Text = "tomatoes"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
End With
'
Selection.Find.Execute
Selection.InsertParagraphAfter
Repeat (2)

在我的例子中,只有一个搜索结果是正确的,并且 return 结果。

所以这个想法是这样的...

使用:

Dim fnd As Boolean
fnd = False
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
    Do While .Execute(FindText:="potatoes", Forward:=True, _
        MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=True) = True
        fnd = True
        Selection.MoveRight Unit:=wdCharacter, Count:=3
        Selection.InsertAfter vbCr
        Selection.Collapse wdCollapseEnd
    Loop
End With
If fnd = False Then
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        Do While .Execute(FindText:="tomatoes", Forward:=True, _
        MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=True) = True
            Selection.InsertAfter vbCr
            Selection.Collapse wdCollapseEnd
        Loop
    End With
End If