简体   繁体   中英

How to fix “Execution Error 429 : ActiveX can't create object” error in VBA

This Excel macro below should convert all .Docx into the selected folder to .Pdf

Here is the code line which provide me the error Code 429, But several hours ago this same line of code was working.

Documents.Open (filePath & currFile) 'Error Code 429

Here the full macro's code

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

Is there a simple way to make this macro working and to fix this error.

Best regards.

Documents.Open is a method of the Documents object, which needs the "MS Word Object Library" to function, without being referred to a word object explicitly:

在此输入图像描述

What does this mean? If the Microsoft Word 1X.0 reference is checked (VBE>Extras>Libraries), then the code below works quite ok:

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

If the "MS Word Object Library" is not referred, then with late binding it still could be referred to the object. (Late binding is 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

If needed, Documents.Open may return a document object:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM