PDA

View Full Version : Open PDF file after creating multiple pages into one PDF



roy.caussy
07-02-2014, 03:32 AM
Hi i have the below code and i wanting for it to open the pdf after creating the pdf. At the minute it just creates the single pdf file. ( I have no idea about VBA, i found this online but been spending days figuring how to amend this!)
THanks for your help
Option Explicit
Sub PrintToPDF_SpecifiedSheetsToOne_Early()
'write RC
Dim pdfjob As PDFCreator.clsPDFCreator
Dim sPDFName As String
Dim sPDFPath As String
Dim sSheetsToPrint As String
Dim sSheets() As String
Dim lSheet As Long
Dim lTtlSheets As Long
Dim bRestart As Boolean
'/// Change the output file name here! ///''''''change the name output again maybe option RC...
sPDFName = "Consolidated.pdf"
sPDFPath = ActiveWorkbook.Path & Application.PathSeparator

'/// Record the sheets you want to print here! ///
'/// Use sheet names separated by commas only ///
sSheetsToPrint = "Sheet1,Sheet3"

'Activate error handling and turn off screen updates
On Error GoTo EarlyExit
Application.ScreenUpdating = False
Set pdfjob = New PDFCreator.clsPDFCreator
'Check if PDFCreator is already running and attempt to kill the process if so................
Do
bRestart = False
Set pdfjob = New PDFCreator.clsPDFCreator
If pdfjob.cStart("/NoProcessingAtStartup") = False Then
'PDF Creator is already running. Kill the existing process numpty,,,RC
Shell "taskkill /f /im PDFCreator.exe", vbHide
DoEvents
Set pdfjob = Nothing
bRestart = True
End If
Loop Until bRestart = False
'Assign settings for PDF job.......rc
With pdfjob
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With

'Split the sheets into an array
sSheets() = Split(sSheetsToPrint, ",")
'Delete the PDF if it already exists
If Dir(sPDFPath & sPDFName) = sPDFName Then Kill (sPDFPath & sPDFName)
'Print the document to PDF
For lSheet = LBound(sSheets) To UBound(sSheets)
On Error Resume Next 'To deal with chart sheets
If Not IsEmpty(Application.Sheets(sSheets(lSheet)).UsedRange) Then
Application.Sheets(sSheets(lSheet)).PrintOut copies:=1, ActivePrinter:="PDFCreator"
lTtlSheets = lTtlSheets + 1

End If
On Error GoTo EarlyExit
Next lSheet
'Wait until all print jobs have entered the print queue
Do Until pdfjob.cCountOfPrintjobs = lTtlSheets
DoEvents
Loop
'Combine all PDFs into a single file and stop the printer
With pdfjob
.cCombineAll
.cPrinterStop = False

End With
'Wait until the file shows up before closing PDF Creator
Do
DoEvents

Loop Until Dir(sPDFPath & sPDFName) = sPDFName
Cleanup:
'Release objects and terminate PDFCreator
Set pdfjob = Nothing
Shell "taskkill /f /im PDFCreator.exe", vbHide
On Error GoTo 0
Application.ScreenUpdating = True
Exit Sub
EarlyExit:
'Inform user of error, and go to cleanup section
MsgBox "There was an error encountered. PDFCreator has" & vbCrLf & _
"has been terminated. Please try again.", _
vbCritical + vbOKOnly, "Error"
Resume Cleanup
End Sub

westconn1
07-02-2014, 02:46 PM
ideally use the shellexecute API, just pass the new file path to the api, the file will open in the default program for that file type

if you want to use a specific pdf program, rather than the default one, you should use shell, passing the program path and filepath as parameters, for shell paths with spaces should be enclosed in ""

there will be examples of both in the forums or google

roy.caussy
07-03-2014, 12:40 AM
Hi thanks for the pointer.
I have been looking at the shellexecute but it seems that you have to define a path for it. Once i finish creating the spreadsheet different users will be storing it in different location so when the pdf is created it will have a different path for different users, is there a way in my current code to include the shell function ie to open the pdf document once the document is created.
Hope this makes sense!
Any help appreciated.
Thanks

snb
07-03-2014, 12:54 AM
why not using Excel's buitlin PDF-facility ?

roy.caussy
07-03-2014, 01:06 AM
because the output pdf from my above code is a printed report it only print and combine the pdf at once with specific project name. I assume you are talkin about the file save as pdf option that will only save one worksheet i need mutile sheets printed into a report pdf format. Thanks

westconn1
07-03-2014, 02:13 AM
the path to the pdf is the same path it is saved to, seems it should be
sPDFPath & "\" & sPDFName

Zack Barresse
07-04-2014, 06:34 PM
You can save the entire workbook as a PDF. From the workbook object, use the ExportAsFixedFormat method, with the Type parameter set to xlTypePDF. You can also open it as a parameter too.