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

标签 excel syntax-error ms-word vba

我正在使用下面的代码在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/

相关文章:

java - 使用 jxl(java) 为 HYPERLINK 编写 excel 公式失败

excel - 使用 python 将数据(列表)按行写入一个 Excel 文件

php - 警告: Cannot use a scalar value as an array

macos - Mac OS X 上的 Word 文档 (.docx) 编辑器

excel - For 循环出现数组下标超出范围错误

javascript - 在远程 Angular 应用程序中下载时 Excel 文件损坏

jquery - 无法通过Ajax请求获取CSV文本

r - ggplot2 : Can't add ` ` to a ggplot object

vba - 用户类型未定义工作表 VBA

python - 如何使用 Python 使用正则表达式从 Word 文档中提取问题