挡板运行时错误91

我是新来的VBA,search遍布的地方,但似乎无法find解决办法。 我得到一个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 

这一行是不正确的:

  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”,并错误地有一个不正确的文件名,并没有抛出一个错误。