![](/img/trans.png)
[英]How to fix "Run time error 429: ActiveX Component Can't Create Object" Error when using Task Scheduler to run a selenium vba macro in Excel?
[英]How to fix “Execution Error 429 : ActiveX can't create object” error in VBA
下面的Excel宏應將所有.Docx轉換為選定的文件夾為.Pdf
這是代碼行,它為我提供了錯誤代碼429,但幾個小時前,同樣的代碼行正在運行。
Documents.Open (filePath & currFile) 'Error Code 429
這里是完整宏的代碼
Sub ConvertDocxInDirToPDF()
Dim filePath As String
Dim currFile As String
filePath = ActiveWorkbook.Path & "\"
MsgBox filePath
currFile = Dir(filePath & "*.docx")
Do While currFile <> ""
Documents.Open (filePath & currFile) 'Error Code 429
Documents(currFile).ExportAsFixedFormat _
OutputFileName:=filePath & Left(currFile, Len(currFile) - Len(".docx")) & ".pdf", _
ExportFormat:=17
Documents(currFile).Close
currFile = Dir()
Loop
Application.ScreenUpdating = True
End Sub
是否有一種簡單的方法可以使此宏工作並修復此錯誤。
最好的祝福。
Documents.Open
是Documents對象的一種方法,它需要“MS Word對象庫”才能運行,而不是明確地引用一個單詞對象:
這是什么意思? 如果選中Microsoft Word 1X.0
引用(VBE> Extras> Libraries),則下面的代碼工作正常:
Sub TestMe()
Dim filePath As String
filePath = ThisWorkbook.Path & "\"
Dim currFile As String
currFile = Dir(filePath & "*.docx")
Dim wrdDoc As Object
Documents.Open filePath & currFile
End Sub
如果沒有引用“MS Word對象庫”,那么通過后期綁定,它仍然可以被引用到該對象。 (后期綁定是CreateObject("Word.Application")
):
Sub TestMe()
Dim filePath As String
filePath = ThisWorkbook.Path & "\"
Dim currFile As String
currFile = Dir(filePath & "*.docx")
Dim wrdApps As Object
Set wrdApps = CreateObject("Word.Application")
wrdApps.Documents.Open (filePath & currFile)
End Sub
如果需要, Documents.Open
可能會返回一個文檔對象:
Sub TestMe()
Dim filePath As String
filePath = ThisWorkbook.Path & "\"
Dim currFile As String
currFile = Dir(filePath & "*.docx")
Dim wrdApps As Object
Set wrdApps = CreateObject("Word.Application")
Dim wrdDoc As Object
Set wrdDoc = wrdApps.Documents.Open(filePath & currFile)
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.