我在一列中有一个动态搜索词列表。我想打开一个Word文档,然后拉回所有找到的单词。由于某种原因,在找到第一个匹配项之后,它将继续执行代码,并且不会再撤回任何匹配项。它应该撤回6个单词,但它仅撤回动态列表中的第一个单词。有什么建议么?这是我的代码:
Sub SearchWord()
Dim odoc As Document
Dim path As String
Dim rng As Word.Range
path = "*MYFILEPATH*"
Dim DS As Worksheet
Dim SS As Worksheet
Set DS = Sheets("Report")
Set SS = Sheets("Search Index")
With SS
SSlastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
End With
With DS
dslastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Set odoc = Documents.Open(Filename:=path)
Set rng = odoc.Content
For J = 2 To SSlastRow
sText = SS.Range("B" & J).Value
With rng.Find
.MatchCase = False
.Text = sText
End With
rng.Find.Execute
If rng.Find.found = True Then
DS.Range("Q" & 2).Value = DS.Range("Q" & 2).Value & sText & ";" & " "
Else
DS.Range("Q" & 2).Value = DS.Range("Q" & 2).Value
End If
Next J
odoc.Close wdDoNotSaveChanges
End Sub
答案 0 :(得分:0)
问题出在哪里,您不需要设置rng。您的代码也可以通过其他方式简化。试试:
.slx