gpt4 book ai didi

excel - 使用Excel在Word文档中查找单词时文件名错误

转载 作者:行者123 更新时间:2023-12-03 08:28:33 25 4
gpt4 key购买 nike

我正在使用下面的代码在Excel电子表格中遍历一些数据并打开Word文档。然后,我想循环浏览一个Word文档并查找Excel工作表中的所有单词。直到我尝试在Excel工作表中找到单词,然后收到“错误的文件名”消息,这行之有效。我突出显示了发生错误的下面的行。我确定这是一个语法错误,我只是不知道正确的语法是什么。谢谢您的帮助.......

    Dim MyDB() As String
Dim MyCol() As String
Dim MyDBCnt As Integer
Dim MyColCnt As Integer
Dim DBCnt As Integer
Dim ResRow As Integer
Dim r As Integer
Dim x As Integer
Dim PrevRow As Integer
ResRow = 1
r = 5
x = 1
PrevRow = 4

Do Until Len(Trim(Cells(r, 4))) + Len(Trim(Cells(r, 5))) = 0
DoEvents
ReDim Preserve MyDB(1 To x)

If (Trim(Cells(r, 4)) & "." & Trim(Cells(r, 5))) = (Trim(Cells(PrevRow, 4)) & "." & Trim(Cells(PrevRow, 5))) Then
' do nothing
Else
MyDB(x) = Trim(Cells(r, 4)) & "." & Trim(Cells(r, 5))
x = x + 1
End If

r = r + 1
PrevRow = PrevRow + 1
Loop

x = x - 1
MyDBCnt = x
r = 5
x = 1

Do Until Len(Trim(Cells(r, 4))) + Len(Trim(Cells(r, 5))) = 0
DoEvents
ReDim Preserve MyCol(1 To x)
MyCol(x) = Trim(Cells(r, 6))
r = r + 1
x = x + 1

Loop

x = x - 1
MyColCnt = x

Worksheets("Results").Activate
MyLastRow = Cells.Find("*", [a1], , , xlByRows, xlPrevious).Row
ResRow = MyLastRow

Set WordApp = CreateObject("word.Application")
Set WordDoc = WordApp.Documents.Open("R:\Report Web\SQL Doc.docx")
WordApp.Visible = True
WordDoc.Activate

tmp = WordDoc.Name
Dim j As Integer

DBCnt = 1

With WordApp.Selection
Do Until DBCnt > MyDBCnt
DoEvents
With Documents(WordDoc).Find ***ERROR OCCURS HERE
.Text = MyDB(DBCnt)
j = 0

Do While .Execute(Forward:=True) = True
DoEvents
j = j + 1
Loop
End With

If j > 0 Then
MsgBox MyDB(DBCnt) & " was found " & j & " times."
End If
DBCnt = DBCnt + 1
Loop
End With

最佳答案

查找不是Document对象的有效属性。您需要在Selection或Range对象上使用它。例如:

Dim rngFind as Word.Range
Set rngFind = WordDoc.Content
With rngFind.Find

End With

关于excel - 使用Excel在Word文档中查找单词时文件名错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34665618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com