提问人:Kirk Hansen 提问时间:11/6/2023 最后编辑:braXKirk Hansen 更新时间:11/6/2023 访问量:66
如何知道 Word VBA“查找”在哪里找到了一些东西
How to know where Word VBA 'Find' has found something
问:
在下面的 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
答:
1赞
taller
11/6/2023
#1
找到匹配范围后,将重新定义对象。更多详情Range
Microsoft 文档:
选项 1:
Sub FindWhatever()
Dim Rng As Range
Set Rng = ActiveDocument.Content
With Rng
With .Find
.Forward = True
.Wrap = wdFindStop
.Text = "whatever"
End With
Do While .Find.Execute
' Rng is the object of search result
Rng.Select ' for testing
Stop ' for testing
.Collapse wdCollapseEnd
Loop
End With
End Sub
选项 2:
Sub FindWhatever()
Selection.HomeKey Unit:=wdStory
With Selection
With .Find
.Forward = True
.Wrap = wdFindStop
.Text = "whatever"
End With
Do While .Find.Execute
' Selection is the object of search result
Selection.Select ' for testing
Stop ' for testing
.Collapse wdCollapseEnd
Loop
End With
End Sub
评论
Range
Find