【发布时间】:2018-12-21 20:13:47
【问题描述】:
我有一段代码在文档中从当前选择到结尾搜索单词。这样做的目的是在下次运行时找到下一个实例,依此类推。
它可以正常工作,直到它在表格中找到一个单词,此时它不会在该条目之后找到任何内容。我需要能够在表格和文本中找到单词。它还作为用户表单中的函数运行(无模式运行),等待用户输入然后提供不同的单词,循环并根据用户输入执行操作。所以我不相信我可以在查找部分运行我的其他代码(尽管我很高兴得到纠正)。
Sub test1()
Dim list() As String
Dim wrd As String
Dim mrk As Integer
wrd = "ABC" 'Get next word from list
'set range to search as from current selection (previously found) to end of document
Dim DocRng
Set DocRng = ActiveDocument.Range(Start:=Selection.End, End:=ActiveDocument.Content.End)
mrk = Selection.End 'Mark end of previously found instance (current selection)
With DocRng.Find 'Find next instance of word and select it
.Text = wrd
.MatchCase = True
.Forward = True
.Execute
DocRng.Select
End With
If Selection.End = mrk Then 'If selection hasn't changed inform user and go to start of document
MsgBox ("Reached end of document.")
Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=0
End If
tmp = Selection.Text 'Save currently selected text
End Sub
如何让它找到表格后面的条目?
【问题讨论】:
-
这个讨论中的答案应该会有所帮助。阅读表格中关于逐个单元格地使用查找的说明。 stackoverflow.com/questions/51925805/…