1

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.

1
  • 1
    how are you setting the Documents variable? Did you have a look here: stackoverflow.com/questions/17327327/…, perhaps it'll get you going the right direction... Commented Jun 11, 2019 at 15:17

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you @Vityata , Thanks it solved my problem can you explain me why Documents.open was working and now this don't. I am trying to understand but i guess i miss something here. Regards
@Dorian - you are welcome. I have enlarged the answer, explaining the needed library to use Documents.Open from Excel, without returning an object.
Helle, Now I got the same error but here : Set wrdApps = CreateObject("Word.Application") Strange fact when i press F5 After the error message the code works well how can I fix this ?
@Dorian - strange indeed. Try one of these - create the file in a new word file. Then try one of this - stackoverflow.com/questions/40343021/…
@Viyata , Thanks I just dodged the issue by Killing Word process at the end of each subb using docx. Thanks for all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.