如何知道Word VBA“查找”在哪里找到了某些内容

问题描述 投票:0回答:1

在下面的 While 循环中,我需要创建一个 Range 对象,其 Start 和 End 属性指示“whatever”的当前实例,以便我可以将其传递给某个例程。

此信息不在 ActiveDocument.Range 的 Start 和 End 属性中,而且我还没有在 Find 对象中识别它。我应该去哪里寻找?

    Sub FindWhatever()
      With ActiveDocument.Range
        With .Find
          .Wrap = wdFindStop
          .Text = "whatever"
        End With
        Do While .Find.Execute = True
          '??? Create a Range object indicating this instance of "whatever" ???
          'Pass this Range object to a certain routine
          .Collapse wdCollapseEnd
        Loop
      End With
    End Sub
vba find range
1个回答
0
投票

请尝试一下。

Sub FindWhatever()
    Dim Rng As Range
    Set Rng = ActiveDocument.Content
    With Rng
        With .Find
            .Wrap = wdFindStop
            .Text = "whatever"
        End With
        Do While .Find.Execute = True
            ' Rng is the object of search result
            Rng.Select ' Testing
            Stop
            .Collapse wdCollapseEnd
        Loop
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.