Introduction | Example | Tutorial | Applications

Introduction - VBA Combine PDF Files

In this tutorial, I’m going to show you exactly what you need to do to combine PDF files with VBA. The macro I’ll show you uses Word, Excel, or other Microsoft Office applications to automatically merge PDFs using Adobe Acrobat.

This macro won’t work if you only have the Adobe Reader installed.

Example - VBA Combine PDF Files

Private Function MergePDFs(arrFiles() As String, strSaveAs As String) As Boolean
'---PROGRAM: MergePDFs------------------------------------------------------------------------------
'---DEVELOPER: Ryan Wells (
'---DATE: 09/2017-----------------------------------------------------------------------------------
'---DESCRIPTION: This function uses Adobe Acrobat (won't work with just the Reader!) to-------------
'---             combine PDFs into one PDF and save the new PDF with its own file name.-------------
'---INPUT: The function requires two arguments.-----------------------------------------------------
'---       1) arrFiles is an array of strings containing the full path to each PDF you want to------
'---          combine in the order you want them combined.------------------------------------------
'---       2) strSaveAs is a string containing the full path you want to save the new PDF as.-------
'---REQUIREMENTS: 1) Must add a reference to "Adobe Acrobat X.0 Type Library" or "Acrobat"----------
'---                 under Tools > References. This has been tested with Acrobat 6.0 and 10.0.------
'---CAUTION: This function won't work unless you have the full Adobe Acrobat. In other words,-------
'            Adobe Reader will not work.------------------------------------------------------------
Dim objCAcroPDDocDestination As Acrobat.CAcroPDDoc
Dim objCAcroPDDocSource As Acrobat.CAcroPDDoc
Dim i As Integer
Dim iFailed As Integer
On Error GoTo NoAcrobat:
'Initialize the Acrobat objects
Set objCAcroPDDocDestination = CreateObject("AcroExch.PDDoc")
Set objCAcroPDDocSource = CreateObject("AcroExch.PDDoc")
'Open Destination, all other documents will be added to this and saved with
'a new filename
objCAcroPDDocDestination.Open (arrFiles(LBound(arrFiles))) 'open the first file
'Open each subsequent PDF that you want to add to the original
  'Open the source document that will be added to the destination
    For i = LBound(arrFiles) + 1 To UBound(arrFiles)
        objCAcroPDDocSource.Open (arrFiles(i))
        If objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
          MergePDFs = True
          'failed to merge one of the PDFs
          iFailed = iFailed + 1
        End If
    Next i
objCAcroPDDocDestination.Save 1, strSaveAs 'Save it as a new name
Set objCAcroPDDocSource = Nothing
Set objCAcroPDDocDestination = Nothing
If iFailed <> 0 Then
    MergePDFs = False
End If
On Error GoTo 0
End Function

Make powerful macros with our free VBA Developer Kit

It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free VBA Developer Kit and wrote the Big Book of Excel VBA Macros full of hundreds of pre-built macros to help you master file I/O, arrays, strings and more - grab your free copy below.

I'll take a free VBA Developer Kit

Tutorial - VBA Combine PDF Files

First, let me tell you why this is such a useful macro to have. In Word, you can’t use VBA to save non-adjacent pages to a single PDF. For example, you can’t simply save pages 1, 5, and 10 into a single PDF. (If this statement is wrong and you know a way to do this, please leave a comment telling me how.)

However, you can save your pages individually and, as long as you have Adobe Acrobat installed, you can combine them using the example macro above!

So, how does it work?

Getting Started

This macro works from Word, Excel, and other Microsoft Office applications, but before you can run it, you need to add a reference to the Adobe Acrobat X.0 Type Library.

To add a reference to this type library, follow these steps:

  1. Go to your visual basic editor
  2. Click Tools > References
  3. Scroll down until you see Adobe Acrobat X.0 Type Library, where X is an integer representing the version of Acrobat you have installed. On same machines, this may simply be listed as Acrobat.
  4. Check the box beside the type library and press OK.

Notice that just because you see an Adobe Acrobat X.0 Type Libray in your references, it doesn’t mean you have Acrobat installed. Make sure you actually have Acrobat installed! You may just have the Reader installed, in which case the macro won’t work.

Once you’ve set the references and confirmed you have Acrobat installed, you can copy and paste the MergePDFs example macro into your visual basic editor and go onto the next section of this tutorial.

MergePDFs Input/Output

The MergePDFs function must be passed two arguments:

  1. arrFiles - The first argument as an array of strings containing the full path to each PDF you want to combine in the order you want them combined.
  2. strSaveAs - The second argument is a string representing the full path you want to save the new combined PDF as.

If the macro successfully combines each of your PDFs into one PDF, the MergePDFs function will return a value of True.

If it fails, it will return False. There are a number of reasons the function could return False, but two of the most common reasons are:

  1. One or more of the PDFs you defined in arrFiles doesn’t exist. If that’s the case, the macro will continue to combine the existing PDF files, but it will skip over the non-existent files (of course).
  2. You don’t have Adobe Acrobat installed on your computer or you haven’t added a reference to the “Adobe Acrobat X.0 Type Library.” It’s important to note that just because you see an Adobe Acrobat X.0 Type Libray in your references, it doesn’t mean you have Acrobat installed. You may just have the Reader installed, in which case the macro won’t work.

MergePDFs Demonstration

Let’s combine what we’ve learned in this tutorial into a final demonstration illustrating how to call the MergePDFs function:

Sub Combine_PDFs_Demo()
Dim strPDFs(0 To 2) As String
Dim bSuccess As Boolean
strPDFs(0) = "C:\Users\Ryan\Desktop\Page1.pdf"
strPDFs(1) = "C:\Users\Ryan\Desktop\Page5.pdf"
strPDFs(2) = "C:\Users\Ryan\Desktop\Page10.pdf"
bSuccess = MergePDFs(strPDFs, "C:\Users\Ryan\Desktop\MyNewPDF.pdf")

If bSuccess = False Then MsgBox "Failed to combine all PDFs", vbCritical, "Failed to Merge PDFs"

End Sub

In this example, I store the paths of the 3 PDFs I want to combine into an array named strPDFs. I put the files in the exact order I want them combined. Page 1, then page 5, then page 10.

I want to combine these into a new PDF titled MyNewPDF.pdf. In the demonstration, I supplied this new path string directly in the MergePDFs argument list, but ideally you would choose to pass it as a variable.

Notice I set the MergePDFs function equal to the boolean variable bSuccess. This way, I can check whether or not the PDF successfully combined all 3 files in my strPDFs array.

Modify this macro to fit your needs and try to run it. If it fails, it will generate a message box warning the user that an error was encountered. If it succeeds, all the PDF pages will be merged into one convenient PDF.

Application Ideas

At the beginning of this tutorial, I told you this function is great for combining non-adjacent pages in your Word documents. This is a wonderful application of this function, but there are a number of other ways you may want to use it!

I’ve used it to merge PDFs created by other programs and I’ve even looped through all the PDFs in a folder and combined them all at once.

For more great PDF ideas, check out PDF VBA.

That’s all for this tutorial. When you’re ready to take your VBA to the next level, subscribe using the form below.