添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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()
Application.ScreenUpdating = True
End Sub

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

Best regards.

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... – sous2817 Jun 11, 2019 at 15:17

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
  • Documentation
  • 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 – TourEiffel Jun 11, 2019 at 15:27 @Dorian - you are welcome. I have enlarged the answer, explaining the needed library to use Documents.Open from Excel, without returning an object. – Vityata Jun 11, 2019 at 15:54 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 ? – TourEiffel Jun 17, 2019 at 9:37 @Dorian - strange indeed. Try one of these - create the file in a new word file. Then try one of this - stackoverflow.com/questions/40343021/… – Vityata Jun 17, 2019 at 12:39 @Viyata , Thanks I just dodged the issue by Killing Word process at the end of each subb using docx. Thanks for all – TourEiffel Jun 17, 2019 at 13:27

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.