Printing excel sheet to pdf
Hello
This is my first post in this forum, although I've being around reading some posts which had help me programming some VBA routines in excel (I'm a rookie). I've registered because I've went up against a wall with a problem in a coding I'm adapting. I've searched in web and in this forum I haven't found any similar thread, so here it goes.
I'm creating a routine in a excel workbook that:
1st: prints an excel sheet into a pdf file in directory 'Certificados' with the name of cell O5
2nd: combine the above pdf file with a 2nd pdf file (which have a varying name, according to cell D9 of the above mentioned sheet), located in the same directory. For this I've an private function 'MergePDFs'.
I've installed Adobe DC Pro and I've checked 'Adobe Acrobat 10.0 Type Library' in VBA references linked to a *.tlb file.
The 1st step of the routine works well and the 2nd retrieves the message 'Failed to combine all PDFs'. this could be due to inexisting pdf files to merge or some kind of error with the adobe library. The files are in the folder so I presume this problem in proceeding with the routine should be related with *.tlb file or some *.dll so It's possible to open the pdf files...Eventually this is due to some dumb error :think:
I thank you any help you can provide. Bellow the code.
______________________________
Code:
Private Function MergePDFs(arrFiles() As String, strSaveAs As String) As Boolean
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
Else
' failed to merge one of the PDFs
iFailed = iFailed + 1
End If
objCAcroPDDocSource.Close
Next i
objCAcroPDDocDestination.Save 1, strSaveAs 'Save it as a new name
objCAcroPDDocDestination.Close
Set objCAcroPDDocSource = Nothing
Set objCAcroPDDocDestination = Nothing
NoAcrobat:
If iFailed <> 0 Then
MergePDFs = False
End If
On Error GoTo 0
End Function
Sub Main()
' ExportAsPDF
Dim FolderPath As String
FolderPath = "C:\Users\ITG-0720\Certificados"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderPath & "" & Sheets("Análise CC").Range("O5"), _
openafterpublish:=False, ignoreprintareas:=False
MsgBox "All PDF's have been successfully exported."
' Combine_PDFs()
Dim strPDFs(0 To 1) As String
Dim bSuccess As Boolean
FolderPath = "C:\Users\ITG-0720\Certificados"
strPDFs(0) = FolderPath & "" & Sheets("Análise CC").Range("D9")
strPDFs(1) = FolderPath & "" & Sheets("Análise CC").Range("O5")
bSuccess = MergePDFs(strPDFs, FolderPath & "" & Sheets("Análise CC").Range("O5"))
If bSuccess = False Then MsgBox "Failed to combine all PDFs", vbCritical, "Failed to Merge PDFs"
End Sub
1 Attachment(s)
Merging PDF files in excel VBA
Attachment 23840
Hi! Sorry for not answering sooner but this week we had a carnival holiday...I thank you once again for your availability. I made some changes in the code (including changing the destiny folder) so I'm attachning the excel workbook along with one of the pdf files to be merged ('CL-5208PR-19.pdf'). The other pdf file is generated from the excel sheet 'Análise CC', which generates 'RIS 528.pdf' file. As I said In my previous thread, the code is working fine but it does not allow me to insert the 'RIS 528.pdf' file into 'CL-5208PR-19.pdf' (I would like this one to be the front page). If you can figure why is this happening I would appreciate.