Hi! I was trying to look for a way to merge PDFs with a macro. I saw this (VBA, Combine PDFs into one PDF) and tried to modify but cannot do it.
I want to merge all the pdfs in row 2 together, then rename with the name in E2. The complication is that not every row has the same amount of files to merge and not every month we have the same number of rows.
I tried to run this code. I get an error "Error merging Document21.pdf" as well as "Created C:\Users\Marlowe\Documents\MergeMerge1.pdf" - but no file was created.
Is what I want possible to achieve?
Thanks in advance.
File 1 | File 2 | File 3 | File 4 | Merge Name |
Document1.pdf | Document12.pdf | Merge1.pdf | ||
Document21.pdf | Document22.pdf | Document23.pdf | Merge2.pdf | |
Document31.pd | Document32.pdf | Merge3.pdf | ||
Document41.pdf | Document42.pdf | Document43.pdf | Document44.pdf | Merge4.pdf |
VBA Code:
'References
'Adobe Acrobat 10.0 Type Library
Option Explicit
Public Sub Merge_PDFs()
Dim objCAcroPDDocDestination As Acrobat.CAcroPDDoc
Dim objCAcroPDDocSource As Acrobat.CAcroPDDoc
Dim PDFfiles As Range, PDFfile As Range
Dim n As Long
With ActiveSheet
Set PDFfiles = .Range("A2", .Cells(.Rows.Count, "A").End(xlUp))
End With
'Create Acrobat API objects
Set objCAcroPDDocDestination = CreateObject("AcroExch.PDDoc")
Set objCAcroPDDocSource = CreateObject("AcroExch.PDDoc")
'Open first PDF file and merge other PDF files into it
n = 0
For Each PDFfile In PDFfiles
n = n + 1
If n = 1 Then
objCAcroPDDocDestination.Open PDFfile.Value
Else
objCAcroPDDocSource.Open PDFfile.Value
If Not objCAcroPDDocDestination.InsertPages(objCAcroPDDocDestination.GetNumPages - 1, objCAcroPDDocSource, 0, objCAcroPDDocSource.GetNumPages, 0) Then
MsgBox "Error merging " & PDFfile.Value
End If
objCAcroPDDocSource.Close
End If
Next
'Save merged PDF files as a new file
objCAcroPDDocDestination.Save 1, ThisWorkbook.Path & Range("E2").Value
objCAcroPDDocDestination.Close
Set objCAcroPDDocSource = Nothing
Set objCAcroPDDocDestination = Nothing
MsgBox "Created " & ThisWorkbook.Path & Range("E2").Value
End Sub
I want to merge all the pdfs in row 2 together, then rename with the name in E2. The complication is that not every row has the same amount of files to merge and not every month we have the same number of rows.
I tried to run this code. I get an error "Error merging Document21.pdf" as well as "Created C:\Users\Marlowe\Documents\MergeMerge1.pdf" - but no file was created.
Is what I want possible to achieve?
Thanks in advance.