Search code examples
excelvbaexport-to-pdf

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.


Solution

  • 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:

    enter image description here

    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