2017-07-31 55 views
0

您能檢查一下我在代碼中丟失了什麼嗎? 當它達到wrd.Visible = True時,它給我錯誤「運行時錯誤91:對象變量或未設置塊變量」。我的代碼有什麼問題 - 「運行時錯誤91:對象變量或塊變量未設置」

我已經激活了的Microsoft Word 14.0對象庫

OBL SS

Sub Exceltoword_template() 
'Declares and set w as active worksheet 
Dim w As Worksheet 
Set w = ActiveWorkbook.ActiveSheet 
'Declaration for word app 
Dim wrd As Object 
Dim worddoc As Word.Document 

'Optimize Code 
    Application.ScreenUpdating = False 
    Application.EnableEvents = False 

On Error Resume Next 
'Is MS Word already opened? 
'      Set wrd = GetObject(class:="Word.Application") 
      Set wrd = GetObject(class:="Word.Application") 
'Clear the error between errors 
      Err.Clear 
'If MS Word is not already open then open MS Word 
      If wrd Is Nothing Then Set wrd = CreateObject(class:="Word.Application") 
'     Handle if the Word Application is not found 
     If Err.Number = 429 Then 
     MsgBox "Microsoft Word could not be found, aborting." 
     GoTo EndRoutine 
     End If 

On Error GoTo 0 
'Make MS Word Visible and Active 

wrd.Visible = True 
wrd.Activate 


EndRoutine: 
'Optimize Code 
    Application.ScreenUpdating = True 
    Application.EnableEvents = True 
End Sub 
+0

該錯誤必須來自其他地方 - 代碼功能正常,只要您安裝了Word 2010。 –

+0

在詢問429號碼之前,錯誤號是否高於零? – reporter

+0

@ DarrenBartrup-Cook是的,我使用的是2010版本。 –

回答

0

我一般會建議不要使用早期綁定,儘管它可以與智能感知幫助。我可以通過在引用Word時忽略單詞Set而獲得與您相同的錯誤,但除此之外它可以正常工作。

試試這個代碼;它不需要對Word的引用:

Public Sub Test() 

    Dim oWD As Object 
    Dim oDoc As Object 
    Dim wrkSht As Worksheet 

    Set wrkSht = ThisWorkbook.Worksheets("Sheet1") 'Be precise, don't trust to Active or Selection. 
    Set oWD = CreateWD 

    Set oDoc = oWD.Documents.Add 'Create a new document. 
    'Set oDoc = oWD.ActiveDocument 
    'Set oDoc = oWD.Documents("My Document.docx") 
    'Set oDoc = oWD.Windows("My Document.docx [Compatibility Mode]") '- As it appears on the title bar of the document. 

    With oDoc 
     'Coding for Word 
    End With 

End Sub 

Public Function CreateWD(Optional bVisible As Boolean = True) As Object 

    Dim oTmpWD As Object 

    ''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    'Defer error trapping in case Word is not running. ' 
    ''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    On Error Resume Next 
    Set oTmpWD = GetObject(, "Word.Application") 

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    'If an error occurs then create an instance of Word. ' 
    'Reinstate error handling.       ' 
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    If Err.Number <> 0 Then 
     Err.Clear 
     On Error GoTo ERROR_HANDLER 
     Set oTmpWD = CreateObject("Word.Application") 
    End If 

    oTmpWD.Visible = bVisible 
    Set CreateWD = oTmpWD 

    On Error GoTo 0 
    Exit Function 

ERROR_HANDLER: 
    Select Case Err.Number 

     Case Else 
      MsgBox "Error " & Err.Number & vbCr & _ 
       " (" & Err.Description & ") in procedure CreateWD." 
      Err.Clear 
    End Select 

End Function 
+0

謝謝!有用。我需要稍微改變。請幫幫我。我如何將Set oDoc = oWD.Documents.Add'設置爲'Set oDoc'作爲活動文檔,因爲我的word doc已經打開。有沒有辦法控制或獲得文檔標題的訪問權限? –

+0

我嘗試使用'Set oDoc = oWD.ActiveDocument'。它不適用於已經打開並給我錯誤的文件**這個命令不可用,因爲沒有文檔是打開的**但是,當我使用代碼'Set oDoc = oWD.Documents.Add'添加文檔時,它然後檢測活動文檔。 –

+0

我添加了評論代碼來展示如何分配'oDoc'。 –

0

我發佈此作爲參考。這是非常類似的代碼,我用它來打開Outlook的檢查,並精確地反映你的意圖,只是它更簡化了它。

'requires early binding (reference set for Microsoft Word Object Library) 
'first check if outlook is running and if not open it 
Dim wdApp As Word.Application 

On Error Resume Next 
Set wdApp = GetObject(, "Word.Application") 
On Error GoTo 0 
If wdApp Is Nothing Then Set wdApp = New Word.Application 

我一直在使用這個多年沒有失敗。

相關問題