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
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
Hi,
Set reference to VBE - Tools - References – Acrobat
and try this 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
Last edited by ZVI; 08-27-2013 at 05:19 AM.
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.
Hi there!
Despite setting the correct references, I cannot get this working.
The problem lies in this line
AcrobatError.PNGIt says "Activex component can't create object"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
Last edited by ZVI; 08-21-2014 at 07:26 AM.
Hi,
Sorry no luck. Still the same result.
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.
Hi i am getting the error of user defined type not defined. I am using Excel 2013 and the libraries available for me are
1) Adobe Acrobat browser control type library 1.0
2) Adobe reader file preview
3)Acrobat access 3.0
Capture.PNG
Version is 15.010
please help its very urgent
Last edited by Shiva117; 08-18-2016 at 11:13 AM.
See post 34.
You must have Acrobat installed and not just Adobe Reader.
Hi,
Can we do it using Acrobat reader. if yes please share the code. even if I need to use third part controls please help me on this
Remember To Do the Following....
Use [Code].... [/Code] tags when posting code to the thread.
Mark your thread as Solved if satisfied by using the Thread Tools options.
If posting the same issue to another forum please show the link
Well, then try this one:
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
Hi,
Can you please help me as well, I need to save all the files in a folder(pdf files)(whatever be the name) by converting them to a single file using Adobe to another folder. some of the files in the folder(which are to be converted to a single) are password protected also. please HELP, Thanks!!
Welcome to the forum! Please start your own thread. http://www.vbaexpress.com/forum/newt...newthread&f=17
You can reference this thread if needed. If links can not be posted yet, list thread 47310 as the reference.
I guess you want all the files merged into one file? I guess all the files have the same password? I guess that you mean Adobe Acrobat and NOT Adobe Reader?
Thanks!! I have posted a new thread. And only 3 files are password protected.. I need total 5 files to be converted into one and saved to another location
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.
There are several PDF programs one can use to merge and splice/split PDF files. PDFSam is one. Here is a split example. http://www.vbaexpress.com/forum/show...d-instructions
PDFSam also has a GUI method to do it as well. Before I got Adobe Pro, it used one of those methods.
I also wrote a vb.net file to accept command line parameters to do it using the iTextSharp.dll routines.
There are several other 3rd party PDF programs to can accept command line parameters if you are seeking that kind of solution.