2014-01-23 119 views
2

我是VBA的新手,已經搜遍遍地,但似乎無法找到解決方案。我是 獲得Run-time error 91: Object variable or With block variable not set錯誤。有誰知道爲什麼?擋板運行時錯誤91

非常感謝。

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 
+4

哪條線在拋出錯誤時突出顯示?另外,快速查看您的代碼可以發現您有兩個連續副本和兩個連續粘貼。改爲複製粘貼複製粘貼。在你的代碼中找到'rnRevenueTable.Copy',你就會明白。 – Manhattan

+0

感謝您的回覆。該給錯誤消息中的行是: 集wdbmHeadLossTable = wdDoc.Bookmarks( 「HeadLossTable」)範圍 集wdbmRevenueTable = wdDoc.Bookmarks( 「RevenueTable」)範圍 – user3228092

+0

嘗試改變線格式爲:。wdDoc.Range .Bookmarks( 「HeadLossTable」)。 '書籤'是'Range'的一個屬性。這可能會引發錯誤。 – Manhattan

回答

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) 

我注意到的是用原來的代碼,它沒有拋出呃ror和wdDoc只是設置爲Nothing。我刪除了「sbBook.Path」,並錯誤地輸入了錯誤的文件名,而且確實發生了錯誤。