VBA to Merge PDFs

KatKitKat

New Member
Joined
Apr 27, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I adapted a VBA code found on the message board to merge 6 different PDF files into one PDF, but it is not working and I am lost as to where to look.

I have Adobe Acrobat DC installed but the files are not merging and I do not get an error message, so this is probably something stupid. All the pdf files are saved to the same folder. The file called "myPDFFile1.pdf" is the PDF I wish to merge the other documents into. I have 2 different codes which are below:

VBA Code:
Sub mainCreateFile()

Dim myCol                               As Collection
Dim strFile                             As String
Dim inputDirectoryToScanForFile         As String
Dim primaryFile                         As String

    Set myCol = New Collection

    primaryFile = "C:\Users\KH\OneDrive - CCMI\Desktop\EXPOSURES\myPDFFile1.pdf"

    myCol.Add primaryFile

    inputDirectoryToScanForFile = "C:\Users\KH\OneDrive - CCMI\Desktop\EXPOSURES\"

    strFile = Dir(inputDirectoryToScanForFile & "*.pdf")

    Do While strFile <> ""
        myCol.Add strFile
        strFile = Dir
    Loop
End Sub

and

VBA Code:
Sub mainmerge()

    Dim arrayFilePaths() As Variant
    Set app = CreateObject("Acroexch.app")

    arrayFilePaths = Array("C:\Users\KH\OneDrive - CCMI\Desktop\EXPOSURES\myPDFFile2.pdf", _
                            "C:\Users\KH\OneDrive - CCMI\Desktop\EXPOSURES\myPDFFile3.pdf", _
                            "C:\Users\KH\OneDrive - CCMI\Desktop\EXPOSURES\myPDFFile4.pdf", _
                            "C:\Users\KH\OneDrive - CCMI\Desktop\EXPOSURES\myPDFFile5.pdf", _
                            "C:\Users\KH\OneDrive - CCMI\Desktop\EXPOSURES\myPDFFile6.pdf")

    Set primaryDoc = CreateObject("AcroExch.PDDoc")
    OK = primaryDoc.Open(arrayFilePaths(0))
    Debug.Print "PRIMARY DOC OPENED & PDDOC SET: " & OK

    For arrayIndex = 1 To UBound(arrayFilePaths)
        numPages = primaryDoc.GetNumPages() - 1

        Set sourceDoc = CreateObject("AcroExch.PDDoc")
        OK = sourceDoc.Open(arrayFilePaths(arrayIndex))
        Debug.Print "SOURCE DOC OPENED & PDDOC SET: " & OK

        numberOfPagesToInsert = sourceDoc.GetNumPages

        OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, numberOfPagesToInsert, False)
        Debug.Print "PAGES INSERTED SUCCESSFULLY: " & OK

        OK = primaryDoc.Save(PDSaveFull, arrayFilePaths(0))
        Debug.Print "PRIMARYDOC SAVED PROPERLY: " & OK

        Set sourceDoc = Nothing
    Next arrayIndex

    Set primaryDoc = Nothing
    app.Exit
    Set app = Nothing
    MsgBox "The Use Log is complete"
End Sub

The code runs and I get the pop-up message, but not a merged file.

Thanks for the help.
Kat
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi Kat,

I am SUPER new, so I am not sure if I am helping..
But maybe check to see if you have the Adobe program (the paid version).
If you do, you may need to enable it in Tools->References in VBA.

I am trying to find or put together a vba to merge PDFs myself!
Where I work, they don't want to bother paying for the full Adobe program.
I'll let you know if I am successful!
 
Upvote 0
Hi Alan,

Thanks for this!

I watched the video, and it looks like only the tables from the PDF can be imported.
What I am after is having the full PDF (as is) merged with other PDF files.

Thank you though!
 
Upvote 0

Forum statistics

Threads
1,224,771
Messages
6,180,860
Members
453,003
Latest member
SalihZekiKoni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top