从Excel vba中找不到Word

时间:2014-12-11 19:54:50

标签: excel vba excel-vba ms-word word-vba

我正在尝试从Word中的表中获取信息并将其拉入Excel,因为表的数量是可变的,我没有其他方式来引用表我想在其前面的文本之后直接找到表在文件中。我之前使用过.Find方法,但出于某种原因我这次无法让它工作,而且从我以前读过和使用的所有内容开始,这应该可行:

Sub Process_NIM()
    Dim WordPullFile As Object
    Dim PullFolder As String
    Dim PullDate As Date
    Dim AppWord As Word.Application
    Dim wd As Word.Document
    Dim Table1 As Word.Table
    Dim wb As Workbook
    Dim ws1, ws2 As Worksheet
    Dim Text1 As Word.Range

    PullDate = DateAdd("d", 1, Now())
    PullFolder = "M:\Production Case Files\" & Format(PullDate, "YYYY") & _
        " Production Case Files\" & UCase(Format(PullDate, "MMM")) & _
        "\" & UCase(Format(PullDate, "MMM DD")) & "\"
    On Error GoTo OpenFileError
    Set WordPullFile = Application.FileDialog(msoFileDialogOpen)
    With WordPullFile
        .AllowMultiSelect = False
        .Filters.clear
        .Filters.Add "DOC Files (*.doc)", "*.doc"
        .InitialFileName = PullFolder
        .Show
    End With
    On Error GoTo 0
    If WordPullFile.SelectedItems.Count > 0 Then
        PullFolder = WordPullFile.SelectedItems(1)
    End If

    Set AppWord = CreateObject("Word.Application")
    Set wd = AppWord.Documents.Open(PullFolder)
    wd.Application.Visible = True

    Set wb = ThisWorkbook
    Set ws1 = wb.Worksheets(1)
    Set ws2 = wb.Worksheets(2)

    If Text1.wd.Selection.Find.Execute(findtext:="Generator Outages for Today - Greater than 25MW") = True Then
        'do stuff
    End If

OpenFileError:
    MsgBox ("There was an error opening the word file. Try closing any other instances of word and re-run.")
    Exit Sub

End Sub

我目前在Word中使用它,但我无法从Excel中使用它:

If Text1.Find.Execute(findtext:="RC South Regional Review for") Then
    Text1.InsertAfter (" " & Format(FileDate, "MMMM DD, YYYY"))
End If

1 个答案:

答案 0 :(得分:0)

好消息是,你说你在excel中使用的单词中使用的文本实际上是有效的。问题在于你试图投入的价值,我使用你的代码并且有一段时间我感到难过,因为它似乎不起作用,但是当我用一些纯文本代替你的日期时,它工作得很好,告诉我们我找到了工作。事实证明它只是没有解决Filedate作为日期,所以没有插入任何东西。这是你的代码的一个版本,修改为在我的PC上工作,在找到的字符串之后插入一个日期,你的意思是添加Pulldate而不是Filedate作为要附加的变量吗?

Sub Process_NIM()
    Dim WordPullFile As Object
    Dim PullFolder As String
    Dim PullDate As Date
    Dim AppWord As Word.Application
    Dim wd As Word.Document
    Dim Table1 As Word.Table
    Dim wb As Workbook
    Dim ws1, ws2 As Worksheet
    Dim Text1 As Word.Range

    PullDate = DateAdd("d", 1, Now())
    PullFolder = "D:\Users\Mark\Documents\"
    On Error GoTo OpenFileError
    Set WordPullFile = Application.FileDialog(msoFileDialogOpen)
    With WordPullFile
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "DOC Files (*.doc)", "*.doc"
        .InitialFileName = PullFolder
        .Show
    End With
    On Error GoTo 0
    If WordPullFile.SelectedItems.Count > 0 Then
        PullFolder = WordPullFile.SelectedItems(1)
    End If

    Set AppWord = CreateObject("Word.Application")
    Set wd = AppWord.Documents.Open(PullFolder)
    wd.Application.Visible = True

    Set wb = ThisWorkbook
    Set ws1 = wb.Worksheets(1)
    Set ws2 = wb.Worksheets(2)

    Set Text1 = wd.Range
If Text1.Find.Execute(findtext:="RC South Regional Review for") Then
    Text1.InsertAfter (" " & Format(Date, "MMMM DD, YYYY"))
Else
    MsgBox "could not find text"
End If
wd.Save
wd.Close
AppWord.Quit
Set AppWord = Nothing

Exit Sub
OpenFileError:
    MsgBox ("There was an error opening the word file. Try closing any other instances of word and re-run.")
    Exit Sub

End Sub