令人困惑的运行时错误91

时间:2014-01-23 14:07:59

标签: excel vba excel-vba

我是VBA的新手,已经搜遍了所有地方,但似乎无法找到解决方案。我 收到Run-time error 91: Object variable or With block variable not set错误。有谁知道为什么?

非常感谢。

Option Explicit

Sub Survey()

    'Name of the existing Word doc.
    Const stCoHydroSurveyTemplate As String = "Survey Template.docx"

    'Define Word objects.
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim wdbmHeadLossTable As Word.Range
    Dim wdbmRevenueTable As Word.Range

    'Define Excel objects.
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    Dim rnHeadLossTable As Range
    Dim rnRevenueTable As Range

    'Initialize the Excel objects.
    Set wbBook = ThisWorkbook
    Set wsSheet = wbBook.Worksheets("Sheet1")
    Set rnHeadLossTable = wsSheet.Range("HeadLossTable")
    Set rnRevenueTable = wsSheet.Range("RevenueTable")

    'Initialize the Word objets.
    Set wdApp = New Word.Application
    Set wdDoc = wdApp.Documents.Open(wbBook.Path & "D:\Surveys" & stSurveyTemplate)
    Set wdbmHeadLossTable = wdDoc.Bookmarks("HeadLossTable").Range
    Set wdbmRevenueTable = wdDoc.Bookmarks("RevenueTable").Range

    'If the macro has been run before, clean up any artifacts before trying to paste the table in again.
    On Error Resume Next
    With wdDoc.InlineShapes(1)
        .Select
        .Delete
    End With
    On Error GoTo 0

    'Turn off screen updating.
    Application.ScreenUpdating = False

    'Copy the Head Loss Table to the clipboard.
    rnHeadLossTable.Copy
    rnRevenueTable.Copy

    'Select the range defined by the "HeadLossTable" bookmark and paste in from the clipboard to the word doc "Survey Template".
    With wdbmHeadLossTable
        .Select
        .PasteSpecial Link:=True, _
                      DataType:=wdPasteMetafilePicture, _
                      Placement:=wdInLine, _
                      DisplayAsIcon:=False
    End With

    With wdbmRevenueTable

        .Select
        .PasteSpecial Link:=True, _
                      DataType:=wdPasteMetafilePicture, _
                      Placement:=wdInLine, _
                      DisplayAsIcon:=False
    End With



    'Save and close the Word doc.
    With wdDoc
        .Save
        .Close
    End With

    'Quit Word.
    wdApp.Quit

    'Null out your variables.
    Set wdbmHeadLossTable = Nothing
    Set wdbmRevenueTable = Nothing
    Set wdDoc = Nothing
    Set wdApp = Nothing

    'Clear out the clipboard, and turn screen updating back on.
    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With

    MsgBox "The Survey has successfully been " & vbNewLine & _
           "transferred to " & stSurveyTemplate, vbInformation


End Sub

1 个答案:

答案 0 :(得分:0)

此行不正确:

    Set wdDoc = wdApp.Documents.Open(wbBook.Path & "D:\Surveys" & stSurveyTemplate)

也许你的意思是这样:

    Set wdDoc = wdApp.Documents.Open("D:\Surveys" & stSurveyTemplate)

或者这个:

    Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\Surveys" & stSurveyTemplate)

我注意到的是,使用原始代码时,它没有抛出错误,而wdDoc只是设置为Nothing。我删除了“sbBook.Path”,但错误地输入了错误的文件名,这确实引发了错误。