none
Can I use VBA to Convert PDF Files to Word Docs or Text Files? RRS feed

  • Question

  • I'm wondering if there is a way to use VBA to convert a bunch of PDF files into Word documents.  I do NOT have Adobe Acrobat installed; I only have Adobe Reader.  BTW, I need to loop through lots of PDF files in a single folder.  If someone here even has a VBA script to convert a PDF to a Word doc, please share it.  Or, if someone has some VBA code to loop through PDF files and convert each to a Text file, please do share some sample code.

    Thanks everyone.


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

    Wednesday, May 13, 2015 2:59 PM

Answers

  • ​Hi ryguy72,

    >> I'm wondering if there is a way to use VBA to convert a bunch of PDF files into Word documents.

    At present, Word 2013 offers the function to open pdf files directly, and then you could view the pdf in word. The code below shows a simple demo to open pdf files, and save it as word, you could modify it according your own requirement.

    Sub convertToWord()
       Dim MyObj As Object, MySource As Object, file As Variant
       file = Dir("D:\OfficeDev\Word\201505\Pdf\" & "*.pdf") 'pdf path
       Do While (file <> "")
       ChangeFileOpenDirectory "D:\OfficeDev\Word\201505\Pdf\" 
              Documents.Open FileName:=file, ConfirmConversions:=False, ReadOnly:= _
            False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
            "", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
            Format:=wdOpenFormatAuto, XMLTransform:=""
        ChangeFileOpenDirectory "D:\OfficeDev\Word\201505\Pdf\Word"'path for saving word
        ActiveDocument.SaveAs2 FileName:=Replace(file, ".pdf", ".docx"), FileFormat:=wdFormatXMLDocument _
            , LockComments:=False, Password:="", AddToRecentFiles:=True, _
            WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
             SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
            False, CompatibilityMode:=15
        ActiveDocument.Close
         file = Dir
       Loop
    End Sub

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 14, 2015 9:58 AM

All replies

  • ​Hi ryguy72,

    >> I'm wondering if there is a way to use VBA to convert a bunch of PDF files into Word documents.

    At present, Word 2013 offers the function to open pdf files directly, and then you could view the pdf in word. The code below shows a simple demo to open pdf files, and save it as word, you could modify it according your own requirement.

    Sub convertToWord()
       Dim MyObj As Object, MySource As Object, file As Variant
       file = Dir("D:\OfficeDev\Word\201505\Pdf\" & "*.pdf") 'pdf path
       Do While (file <> "")
       ChangeFileOpenDirectory "D:\OfficeDev\Word\201505\Pdf\" 
              Documents.Open FileName:=file, ConfirmConversions:=False, ReadOnly:= _
            False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
            "", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
            Format:=wdOpenFormatAuto, XMLTransform:=""
        ChangeFileOpenDirectory "D:\OfficeDev\Word\201505\Pdf\Word"'path for saving word
        ActiveDocument.SaveAs2 FileName:=Replace(file, ".pdf", ".docx"), FileFormat:=wdFormatXMLDocument _
            , LockComments:=False, Password:="", AddToRecentFiles:=True, _
            WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
             SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
            False, CompatibilityMode:=15
        ActiveDocument.Close
         file = Dir
       Loop
    End Sub

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, May 14, 2015 9:58 AM
  • Wow.  Thanks so much.  I just tried your script and it doesn't appear to work on Word 2010, which is all I have on my work machine.  I have 2013 at home.  I'll try it when I get home tonight, and let you know for sure. 

    I think this will work...


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

    Thursday, May 14, 2015 2:12 PM
  • Hi ryguy

    This will only work in 2013 and later as the capability to open PDF files was introduced in Word 2013.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, May 19, 2015 5:58 PM
    Moderator
  • Yes, it works great on my Office 2013 setup!! 

    Thanks Edward!!


    Knowledge is the only thing that I can give you, and still retain, and we are both better off for it.

    Tuesday, May 26, 2015 2:01 AM
  • Hi Tao Zhou

    I'm from Brazil, Could you give another link to participate the survey? Because the page is not found.

    Thank you


    Thursday, January 11, 2018 1:18 PM
  • Yes, it works great on my Office 2016 setup!! 

    I add curdir to avoid the hardcode, and change to the open word sub.

    Thanks a lot

    Fantastic job Edward!!

    Private Sub Document_Open()
       Dim MyObj As Object, MySource As Object, file As Variant, v_Dir As String
       Application.ScreenUpdating = False
       v_Dir = CurDir()
       file = Dir(v_Dir & "\*.pdf") 'pdf path
       Do While (file <> "")
       ChangeFileOpenDirectory v_Dir
              Documents.Open FileName:=file, ConfirmConversions:=False, ReadOnly:= _
            False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
            "", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
            Format:=wdOpenFormatAuto, XMLTransform:=""
        On Error GoTo endwhile
        ChangeFileOpenDirectory v_Dir 'path for saving word
        ActiveDocument.SaveAs2 FileName:=Replace(file, ".pdf", ".docx"), FileFormat:=wdFormatXMLDocument _
            , LockComments:=False, Password:="", AddToRecentFiles:=True, _
            WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
             SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
            False, CompatibilityMode:=15
        ActiveDocument.Close
    
         file = Dir
       Loop
    endwhile:
        Application.ScreenUpdating = False
        MsgBox "Convertidos.", vbOKOnly
        Me.Close False
    End Sub

    Tuesday, June 5, 2018 3:48 PM
  • Dear,

    I've tried to implement code in Access but get message that sub or function didn't define.

    Position was on ChangeFileOpenDirectory.

    Some help.

    For me will be perfect if there is any code which will be able to convert all pdf from folder to csv or txt.

    I need to define procedure which will read those csv and pick some that to create correlation table .

    PDF isn't so useful but I've noticed that data are always on same position.

    Saturday, August 25, 2018 10:14 PM