There are at least a couple of ways to do this, but the one that I worked up is shown below:
Sub PrintToPDF_DualCopy() 'Author: Ken Puls (www.excelguru.ca) 'Macro Purpose: Print multiple worksheets to individual files and write the output to two locations ' (Download from http://sourceforge.net/projects/pdfcreator/) ' Designed for early bind, set reference to PDFCreator Dim pdfjob As PDFCreator.clsPDFCreator Dim sPDFName As String Dim sPDFPath As String Dim sPDFPath1 As String Dim sPDFPath2 As String Dim lSheet As Long Dim lLoop As Long Dim bRestart As Boolean 'Set your PDF Paths here: sPDFPath1 = ThisWorkbook.Path & Application.PathSeparator sPDFPath2 = "F:\Some Folder\" '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 (or still) 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 Shell "taskkill /f /im PDFCreator.exe", vbHide DoEvents Set pdfjob = Nothing bRestart = True End If Loop Until bRestart = False For lSheet = 1 To ActiveWorkbook.Sheets.Count 'Check if worksheet is empty and skip if so If Not IsEmpty(Worksheets(lSheet).UsedRange) Then sPDFName = "testPDF" & Sheets(lSheet).Name With pdfjob '/// Change the output file name here! /// .cOption("UseAutosave") = 1 .cOption("UseAutosaveDirectory") = 1 .cOption("AutosaveFormat") = 0 ' 0 = PDF .cOption("AutosaveFilename") = sPDFName .cClearCache End With For lLoop = 1 To 2 If lLoop = 1 Then sPDFPath = sPDFPath1 Else sPDFPath = sPDFPath2 End If pdfjob.cOption("AutosaveDirectory") = sPDFPath 'Print the document to PDF Worksheets(lSheet).PrintOut copies:=1, ActivePrinter:="PDFCreator" 'Wait until the print job has entered the print queue Do Until pdfjob.cCountOfPrintjobs = 1 DoEvents Loop pdfjob.cPrinterStop = False 'Wait until the file shows up before closing PDF Creator Do DoEvents Loop Until Dir(sPDFPath & sPDFName & "*.pdf") = sPDFName & ".pdf" Next lLoop End If Next lSheet Cleanup: 'Release objects and terminate PDFCreator On Error Resume Next pdfjob.cClose 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
Some important things to notice about this code:
- It was developed using PDFCreator 0.9.3, and may not work with 0.9.5 as some of the Object Model in that version is different.
- You need to set a reference to the PDFCreator library, as this routine is developed to work with an Early Bind
In addition, this routine incorporates some different methods from my original routines posted on my site, including:
- It attempts to shut down PDFCreator if it is already running when the routine is launched, so that it can create a fresh copy
- It uses a different method to test for completion. Namely, it tests when the file shows up at the target location, rather than testing to see when the PDF queue is empty. (This has inherently been more reliable than the method I originally used.)
Now, I just need to find the time to update all the existing articles on my site, and start playing with version 0.9.5 for Vista...
Note: PDFCreator 0.9.3 doesn't work on Vista, and the Taskkill method may not work on XP Home.