title: VBA, Combine PDFs into one PDF file
body: <p>I am trying to combine PDF's into one single pdf with the use of vba.
  I would like to not use a plug in tool and have tried with acrobat api below.</p>
  
  <p>I have tried something like, but cannot seem to get it to work. I get no error msg but perhaps I am missing parts.</p>
  
  <p>Any help would be appreciated.</p>
  
  <pre><code>   Sub Combine()
  
  
     Dim n As Long, PDFfileName As String
  
      n = 1
      Do
          n = n + 1
          PDFfileName = Dir(ThisWorkbook.Path &amp; "firstpdf" &amp; n &amp; ".pdf")
          If PDFfileName &lt;&gt; "" Then
              'Open the source document that will be added to the destination
              objCAcroPDDocSource.Open ThisWorkbook.Path &amp; "pathwithpdfs" &amp; PDFfileName
              If objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
                  MsgBox "Merged " &amp; PDFfileName
              Else
                  MsgBox "Error merging " &amp; PDFfileName
              End If
              objCAcroPDDocSource.Close
          End If
      Loop While PDFfileName &lt;&gt; ""
  
  
     End Sub
  </code></pre>
  
  <p>new code:</p>
  
  <p>New Code:</p>
  
  <pre><code>Sub main()
  
      Dim arrayFilePaths() As Variant
      Set app = CreateObject("Acroexch.app")
  
      arrayFilePaths = Array("mypath.pdf", _
                              "mypath2.pdf")
  
      Set primaryDoc = CreateObject("AcroExch.PDDoc")
      OK = primaryDoc.Open(arrayFilePaths(0))
      Debug.Print "PRIMARY DOC OPENED &amp; PDDOC SET: " &amp; 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 &amp; PDDOC SET: " &amp; OK
  
          numberOfPagesToInsert = sourceDoc.GetNumPages
  
          OK = primaryDoc.InsertPages(numPages, sourceDoc, 0, numberOfPagesToInsert, False)
          Debug.Print "PAGES INSERTED SUCCESSFULLY: " &amp; OK
  
          OK = primaryDoc.Save(PDSaveFull, arrayFilePaths(0))
          Debug.Print "PRIMARYDOC SAVED PROPERLY: " &amp; OK
  
          Set sourceDoc = Nothing
      Next arrayIndex
  
      Set primaryDoc = Nothing
      app.Exit
      Set app = Nothing
      MsgBox "DONE"
  End Sub
  </code></pre>