Loop over PDF files and transform them into doc with word

后端 未结 2 486
生来不讨喜
生来不讨喜 2021-01-17 17:02

I am trying to use VBA coding - which I am pretty new to - to obtain a series of .doc documents from PDFs (which are not images), that is, I am trying to loop over various P

2条回答
  •  无人共我
    2021-01-17 17:33

    As others have stated, the problem seems to lie mostly with the path & file name. Here is the second version of the code you posted with some changes.

    Unfortunately, a warning message pops up and setting DisplayAlerts to false will not suppress it. But if you click the "don't show this message again" checkbox the first time it pops up, then it will not continue to pop up for every file.

    Sub convertToWord()
    
        Dim MyObj       As Object
        Dim MySource    As Object
        Dim file        As String
        Dim path        As String
    
        path = "C:\Users\username\work_dir_example\"
        file = Dir(path & "*.pdf")
    
        Do While (file <> "")
            Documents.Open FileName:=path & file
            With ActiveDocument
                .SaveAs2 FileName:=Replace(path & file, ".pdf", ".docx"), _
                                    FileFormat:=wdFormatXMLDocument
                .Close
            End With
            file = Dir
        Loop
    
    End Sub
    

提交回复
热议问题