Hi,
I need a code to merge all the PDF files in a folder using adobe acrobat X full version. I will provide the link to the folder in Range E3. Can anybody help me with this.
Thanks:yes
Hi,
I need a code to merge all the PDF files in a folder using adobe acrobat X full version. I will provide the link to the folder in Range E3. Can anybody help me with this.
Thanks:yes
Hi,
Set reference to VBE - Tools - References – Acrobat
and try this code:
Code:Sub MergePDFs()
' ZVI:2013-08-27 http://www.vbaexpress.com/forum/showthread.php?47310-Need-code-to-merge-PDF-files-in-a-folder-using-adobe-acrobat-X
' Reference required: "VBE - Tools - References - Acrobat"
' --> Settings, change to suit
Const MyPath = "C:\Temp" ' Path where PDF files are stored
Const MyFiles = "1.pdf,2.pdf,3.pdf" ' List of PDFs to ne merged
Const DestFile = "MergedFile.pdf" ' The name of the merged file
' <-- End of settings
Dim a As Variant, i As Long, n As Long, ni As Long, p As String
Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc
If Right(MyPath, 1) = "\" Then p = MyPath Else p = MyPath & "\"
a = Split(MyFiles, ",")
ReDim PartDocs(0 To UBound(a))
On Error GoTo exit_
If Len(Dir(p & DestFile)) Then Kill p & DestFile
For i = 0 To UBound(a)
' Check PDF file presence
If Dir(p & Trim(a(i))) = "" Then
MsgBox "File not found" & vbLf & p & a(i), vbExclamation, "Canceled"
Exit For
End If
' Open PDF document
Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
PartDocs(i).Open p & Trim(a(i))
If i Then
' Merge PDF to PartDocs(0) document
ni = PartDocs(i).GetNumPages()
If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 0, ni, True) Then
MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled"
End If
' Calc the number of pages in the merged document
n = n + ni
' Release the memory
PartDocs(i).Close
Set PartDocs(i) = Nothing
Else
' Calc the number of pages in PartDocs(0) document
n = PartDocs(0).GetNumPages()
End If
Next
If i > UBound(a) Then
' Save the merged document to DestFile
If Not PartDocs(0).Save(PDSaveFull, p & DestFile) Then
MsgBox "Cannot save the resulting document" & vbLf & p & DestFile, vbExclamation, "Canceled"
End If
End If
exit_:
' Inform about error/success
If Err Then
MsgBox Err.Description, vbCritical, "Error #" & Err.Number
ElseIf i > UBound(a) Then
MsgBox "The resulting file is created:" & vbLf & p & DestFile, vbInformation, "Done"
End If
' Release the memory
If Not PartDocs(0) Is Nothing Then PartDocs(0).Close
Set PartDocs(0) = Nothing
' Quit Acrobat application
AcroApp.Exit
Set AcroApp = Nothing
End Sub
Hi thanks for the code. Only one problem in Const MyFiles i cannot name all the PDF files because there are many files and will change every time. Can this be done in such a way that all the pdf files in the folder will be merged.
Well, then try this one:
Code:Sub Main()
Const DestFile As String = "MergedFile.pdf" ' <-- change to suit
Dim MyPath As String, MyFiles As String
Dim a() As String, i As Long, f As String
' Choose the folder or just replace that part by: MyPath = Range("E3")
With Application.FileDialog(msoFileDialogFolderPicker)
'.InitialFileName = "C:\Temp\"
.AllowMultiSelect = False
If .Show = False Then Exit Sub
MyPath = .SelectedItems(1)
DoEvents
End With
' Populate the array a() by PDF file names
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
ReDim a(1 To 2 ^ 14)
f = Dir(MyPath & "*.pdf")
While Len(f)
If StrComp(f, DestFile, vbTextCompare) Then
i = i + 1
a(i) = f
End If
f = Dir()
Wend
' Merge PDFs
If i Then
ReDim Preserve a(1 To i)
MyFiles = Join(a, ",")
Application.StatusBar = "Merging, please wait ..."
Call MergePDFs(MyPath, MyFiles, DestFile)
Application.StatusBar = False
Else
MsgBox "No PDF files found in" & vbLf & MyPath, vbExclamation, "Canceled"
End If
End Sub
Sub MergePDFs(MyPath As String, MyFiles As String, Optional DestFile As String = "MergedFile.pdf")
' ZVI:2013-08-27 http://www.vbaexpress.com/forum/showthread.php?47310-Need-code-to-merge-PDF-files-in-a-folder-using-adobe-acrobat-X
' Reference required: VBE - Tools - References - Acrobat
Dim a As Variant, i As Long, n As Long, ni As Long, p As String
Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc
If Right(MyPath, 1) = "\" Then p = MyPath Else p = MyPath & "\"
a = Split(MyFiles, ",")
ReDim PartDocs(0 To UBound(a))
On Error GoTo exit_
If Len(Dir(p & DestFile)) Then Kill p & DestFile
For i = 0 To UBound(a)
' Check PDF file presence
If Dir(p & Trim(a(i))) = "" Then
MsgBox "File not found" & vbLf & p & a(i), vbExclamation, "Canceled"
Exit For
End If
' Open PDF document
Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
PartDocs(i).Open p & Trim(a(i))
If i Then
' Merge PDF to PartDocs(0) document
ni = PartDocs(i).GetNumPages()
If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 0, ni, True) Then
MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled"
End If
' Calc the number of pages in the merged document
n = n + ni
' Release the memory
PartDocs(i).Close
Set PartDocs(i) = Nothing
Else
' Calc the number of pages in PartDocs(0) document
n = PartDocs(0).GetNumPages()
End If
Next
If i > UBound(a) Then
' Save the merged document to DestFile
If Not PartDocs(0).Save(PDSaveFull, p & DestFile) Then
MsgBox "Cannot save the resulting document" & vbLf & p & DestFile, vbExclamation, "Canceled"
End If
End If
exit_:
' Inform about error/success
If Err Then
MsgBox Err.Description, vbCritical, "Error #" & Err.Number
ElseIf i > UBound(a) Then
MsgBox "The resulting file is created:" & vbLf & p & DestFile, vbInformation, "Done"
End If
' Release the memory
If Not PartDocs(0) Is Nothing Then PartDocs(0).Close
Set PartDocs(0) = Nothing
' Quit Acrobat application
AcroApp.Exit
Set AcroApp = Nothing
End Sub
Thanks for the code but i am getting this error under Sub MergePDFs query (Dim a As Variant, i As Long, n As Long, ni As Long, p As String
Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc)
.
Compile error: User-defined type not defined
As it was said in post #2 and has been mentioned in the comments, you need to set reference to VBE - Tools - References – Acrobat.
Press Alt-F11 and choose menu Tools - References, enable checkbox with Acrobat option (may be also Adobe Acrobat ##.# Type Library, where XX.X is version number), and press OK.
ohh yes, so srry. Thanks. Will try now
It's ok :)
By the way, code works with free Adobe Reader because the required library is included in it.
But if your Excel version is 32 bit then install Adobe Reader 32bit. And the same is for 64bit pair versions.
Hey, thanks for the code and it is working fine. But can we do this using adobe acrobat X pro because in acrobat when we do combine files, the merged pdf also shows bookmarks so that we can directly go to a particular pdf.
For referencing to Acrobat X Pro library:
1. Use VBE - Tools - References
2. Uncheck the previously set reference to Acrobat (or Adobe Acrobat ##.# Type Library)
3. Use button Browse to choose the file ACROBAT.TLB in the Acrobat X Pro installation folder (subfolder Acrobat)
4. Press OK
Hi,
I selected ACROBAT.TLB and ran the macro. It worked fine. It merged all the PDFs in to one but i am still not getting Bookmarks in the merged file.
I do get it when i manually merge the files in Acrobat 9 pro (Combine - Merge files in to single PDF).
Is there any way to get the bookmarks as well?
Thanks
May be functionality of Acrobat's Application Program Interface (API) is a bit different to the Graphical User Interface (GUI), which is common practice.
You can ask Adobe support about it.
I just have shown how to use Acrobat's merging API as is.
I browsed for Acrobat.tlb, but when the macro ran, it kept displaying that it cannot insert page from this line: MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled"
Thanks ZVI. I see that the file does have password protection in it even though it does not ask for any when I open it. I tried it with some PDFs I created and the macro worked perfectly.
My next steps will be to only merge similar PDF files (files beginning with same filenames) out of the bunch of PDF file in the folder and then renaming the merged file to a corresponding name on a list. Any tips of the best way to do that?
Name of the destination file is defined in the constant DestFile of the subroutine Main(), change it as required.
Or pass file name you like into the sub MergePDFs() using the 3-rd parameter.
To merge some files in a folder by filename mask replace this f = Dir(MyPath & "*.pdf")
for example by that one f = Dir(MyPath & "MyFileMask*.pdf")
Hi there!
Despite setting the correct references, I cannot get this working.
The problem lies in this line
Attachment 12170It says "Activex component can't create object"Code:Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
Any thoughts?
/Kish
Hi,
Try using early binding: Set PartDocs(i) = New Acrobat.AcroPDDoc
Also follow the recommendations of the post #10
Regards
Hi,
Sorry no luck. Still the same result.:crying:
Regards,
Kish
Hope that Excel version is not Starter Edition which has some major limitations.
Try manually setting the reference to Acrobat X Pro library as it was recommended in post #10.
Try reinstalling the Acrobat X Pro.