您能檢查一下我在代碼中丟失了什麼嗎? 當它達到wrd.Visible = True
時,它給我錯誤「運行時錯誤91:對象變量或未設置塊變量」。我的代碼有什麼問題 - 「運行時錯誤91:對象變量或塊變量未設置」
我已經激活了的Microsoft Word 14.0對象庫
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
該錯誤必須來自其他地方 - 代碼功能正常,只要您安裝了Word 2010。 –
在詢問429號碼之前,錯誤號是否高於零? – reporter
@ DarrenBartrup-Cook是的,我使用的是2010版本。 –