none
Multiple PDF files into word RRS feed

  • Question

  • Good day all experts,

    basically i need two VBA, first turn into word after again turn into PDF(after making some changes)

    i have a folder where is containing almost 100 pdf files, it can be possible that turn into word files by selecting that specific folder. each file has 2 pages and each separately convert and save in same folder..!

    looking forward for your kind response..!!

    Adeel

    Friday, July 21, 2017 6:47 PM

Answers

  • Hello Adeel,

    As I suggested in my previous post, please use Document.SaveAs2 Method (Word) to save into Word files.

    Sub SavePdfToDocx()
    Dim fldr As FileDialog
    Dim sItem As String
    Dim doc As Document
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = "D:"
           If .Show <> 0 Then sItem = .SelectedItems(1)
    End With
    If sItem <> "" Then
    file = Dir(sItem & "\*.pdf")
    Do While (file <> "")
        Set doc = Documents.Open(FileName:=sItem & "\" & file, Visible:=False)   
        doc.SaveAs2 FileName:=sItem & "\" & Split(file, ".")(0) & ".docx" ', FileFormat:=wdFormatPDF
        doc.Close (False)
        file = Dir
    Loop
    End If
    End Sub

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Adeeeel Thursday, July 27, 2017 3:12 PM
    Wednesday, July 26, 2017 1:54 AM
    Moderator
  • Hello,

    To convert docx to pdf, please change the extension in the code above.

    Sub SaveDocxToPdf()
    Dim fldr As FileDialog
    Dim sItem As String
    Dim doc As Document
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = "D:"
           If .Show <> 0 Then sItem = .SelectedItems(1)
    End With
    If sItem <> "" Then
    file = Dir(sItem & "\*.docx")
    Do While (file <> "")
        Set doc = Documents.Open(FileName:=sItem & "\" & file, Visible:=False)   
        'doc.SaveAs2 FileName:=sItem & "\" & Split(file, ".")(0) & ".docx" ', FileFormat:=wdFormatPDF
        doc.SaveAs2 FileName:=sItem & "\" & Split(file, ".")(0) & "Edited.pdf", FileFormat:=wdFormatPDF    
        doc.Close (False)
        file = Dir
    Loop
    End If
    End Sub

    Please do not post several issues in one thread and i suggest you get started from recording a macro. Please visit Create or run a macro.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Adeeeel Thursday, July 27, 2017 3:12 PM
    Thursday, July 27, 2017 2:25 AM
    Moderator

All replies

  • Dear Guru's

    any luck..??

    Adeel

    Saturday, July 22, 2017 6:18 PM
  • refer to these links:

    https://stackoverflow.com/questions/34042575/multiple-word-doc-to-pdf-convert

    https://social.msdn.microsoft.com/Forums/office/en-US/2d3a5cd3-38c3-4811-b3df-4f60848aec54/can-i-use-vba-to-convert-pdf-files-to-word-docs-or-text-files?forum=worddev

    -----------------------------------------

    Create, Read, Edit, Convert & Print Word documents using free .NET Word component.

    Monday, July 24, 2017 3:30 AM
  • Hello Adeel,

    The code below could prompt to select a specific folder and then save each page of Pdf file into a separate file.

    Sub EditPdf()
    Dim fldr As FileDialog
    Dim sItem As String
    Dim doc As Document
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = "D:"
           If .Show <> 0 Then sItem = .SelectedItems(1)
    End With
    If sItem <> "" Then
    file = Dir(sItem & "\*.pdf")
    Do While (file <> "")
        Set doc = Documents.Open(FileName:=sItem & "\" & file, Visible:=False)
        If doc.BuiltInDocumentProperties(wdPropertyPages) = 2 Then
        'Edit the pdf file
        
        'Export each page as PDF
        'Page1
        doc.ExportAsFixedFormat OutputFileName:=sItem & "\" & Split(file, ".")(0) & "Page_1.pdf", _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=1, To:=1, Item:= _
            wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False
        'Page2
        doc.ExportAsFixedFormat OutputFileName:=sItem & "\" & Split(file, ".")(0) & "Page_2.pdf", _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportFromTo, From:=2, To:=2, Item:= _
            wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False
        End If
        doc.Close (False)
        file = Dir
    Loop
    End If
    End Sub
    

    To avoid getting the conversion dialog when opening the Pdf files, please add registry key

    HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\OfficeVersion(15.0 or 16.0)\Word\Options\DisableConvertPdfWarning: 0x00000001

    Reference: Open PDF in Word without Conversion Message

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, July 24, 2017 7:17 AM
    Moderator
  • Hi Celeste

    thanks for reply

    May i didn't explain my need exactly.

    i have folder where 100 plus pdf files, i want all pdf file into word(DOC) files by selection that folder.!

    looking forward for your response.

    Adeel

    Monday, July 24, 2017 10:21 AM
  • Hello,

    >>i want all pdf file into word(DOC) files by selection that folder

    Do you want to merge these pdf file into one word document? If so, we still need to open these files one by one, then copy/paste into one document.

    The code above exports each page as a single pdf file. If you want to open each pdf and then save, please use Document.SaveAs2 Method (Word)

    E.g.

        'Save into pdf
        doc.SaveAs2 FileName:=sItem & "\" & Split(file, ".")(0) & "Edited.pdf", FileFormat:=wdFormatPDF

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 25, 2017 8:45 AM
    Moderator
  • Hi Celeste

    i want each PDF file as each word(DOC) file separately, means if have 100 pdf files in that folder i want 100 word(DOC) files of that pdf, please guide is this possible..!if yes than please help this will reduce my lot of time.


    Adeel

    Tuesday, July 25, 2017 10:24 AM
  • Hello Adeel,

    As I suggested in my previous post, please use Document.SaveAs2 Method (Word) to save into Word files.

    Sub SavePdfToDocx()
    Dim fldr As FileDialog
    Dim sItem As String
    Dim doc As Document
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = "D:"
           If .Show <> 0 Then sItem = .SelectedItems(1)
    End With
    If sItem <> "" Then
    file = Dir(sItem & "\*.pdf")
    Do While (file <> "")
        Set doc = Documents.Open(FileName:=sItem & "\" & file, Visible:=False)   
        doc.SaveAs2 FileName:=sItem & "\" & Split(file, ".")(0) & ".docx" ', FileFormat:=wdFormatPDF
        doc.Close (False)
        file = Dir
    Loop
    End If
    End Sub

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Adeeeel Thursday, July 27, 2017 3:12 PM
    Wednesday, July 26, 2017 1:54 AM
    Moderator
  • Hi Celeste 

    This is great, work like mysterious,i have three bar codes in PDF file which is turn into some strange(due to that data is splitting)can you clear it with VBA(means it can be same like it is in doc file)..? please as requested in my first post, for second VBA that turn again into PDF like above code via selecting only folder, please need your kind help on this..!!

    sample of that pdf files like below link:

    https://www.dropbox.com/s/tp4io3dxiok40q1/NAB.pdf?dl=0

    Adeel

    Wednesday, July 26, 2017 7:26 PM
  • Hello,

    To convert docx to pdf, please change the extension in the code above.

    Sub SaveDocxToPdf()
    Dim fldr As FileDialog
    Dim sItem As String
    Dim doc As Document
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = "D:"
           If .Show <> 0 Then sItem = .SelectedItems(1)
    End With
    If sItem <> "" Then
    file = Dir(sItem & "\*.docx")
    Do While (file <> "")
        Set doc = Documents.Open(FileName:=sItem & "\" & file, Visible:=False)   
        'doc.SaveAs2 FileName:=sItem & "\" & Split(file, ".")(0) & ".docx" ', FileFormat:=wdFormatPDF
        doc.SaveAs2 FileName:=sItem & "\" & Split(file, ".")(0) & "Edited.pdf", FileFormat:=wdFormatPDF    
        doc.Close (False)
        file = Dir
    Loop
    End If
    End Sub

    Please do not post several issues in one thread and i suggest you get started from recording a macro. Please visit Create or run a macro.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Adeeeel Thursday, July 27, 2017 3:12 PM
    Thursday, July 27, 2017 2:25 AM
    Moderator
  • Hi Celeste

    both of working as per my need, please can you see bar code issue once..?

    lot of thanks for your kind help, it will save my lot of time..!thanks you sir

    Adeel

    Thursday, July 27, 2017 3:26 PM