Printing Multiple Word Documents to a Single PDF

This article contains code which will print all open Word documents into a single PDF file.

These code examples are built for PDFCreator, an open source PDF writer utility. Unlike Adobe Acrobat and CutePDF, which both require pro versions to create PDF's via code, PDFCreator is completely free! Download PDF Creator from Sourceforge here. Please note that this code will NOT work with Adobe Acrobat.

It should also be noted that the example in this section use an Early Bind. If you are not familiar with the difference between Early and Late Binding, please read my article on Early vs Late binding.

Versions Tested:

This routine was tested successfully using Word 2003 and PDFCreator 0.9.3 (GPLGhostscript.exe download package), on Windows XP Pro (SP2). It should run just fine in Word 2007 and higher.

NOTE: Before you "go it alone" with trying to adapt any of these routines, you may want to read this article, which shares some of the idiosyncrasies discovered in the development of the PDFCreator code samples.

Setup:

This was tested using the following procedure:

  • Open Word and create two new documents
  • Enter some unique data in each so that you can tell them apart
  • Enter the VBE, create a new module in one of the documents and paste in the code shown below
  • Change the output directory to a valid location
  • Set a reference to PDFCreator
  • Run the code

Once the code has completed, you will have a new PDF with your files printed in the order they were opened.

Interesting Facts:

I was exposed to a couple of weird things in the development of this routine.

  • Background Printing must be turned off. If it isn't, then the routine will end up in an infinite loop until the code is paused, at which point you can run it and it will complete just fine.
  • The word documents actually print to the PDF queue in reverse order, unlike Excel. This made it necessary to institute a reverse loop to print the documents from the most recently opened/created to the oldest in order to have the PDF output document in the expected order.

Code Required

Sub PrintToPDF_MultiWordDoc_to_SingleFile()
'Author       : Ken Puls (www.excelguru.ca)
'Macro Purpose: Print all open Word documents to PDF (static filename)
'
'   (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 sPrinter As String
    Dim lPrintOrder As Long
    Dim lDocs As Long
    Dim bRestart As Boolean
    Dim bBkgrndPrnt As Boolean

    'Activate error handling, capture properties and set req'd settings
    On Error GoTo EarlyExit
    With Application
        sPrinter = CStr(.ActivePrinter)
        .ActivePrinter = "PDFCreator"
        bBkgrndPrnt = .Options.PrintBackground
        .Options.PrintBackground = False
        .ScreenUpdating = False
    End With

    '/// Change the output file name here! ///
    sPDFName = "Consolidated.pdf"
    sPDFPath = "C:Temp"

    '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
            Shell "taskkill /f /im PDFCreator.exe", vbHide
            DoEvents
            Set pdfjob = Nothing
            bRestart = True
        End If
    Loop Until bRestart = False

    'Assign settings for PDF job
    With pdfjob
        .cOption("UseAutosave") = 1
        .cOption("UseAutosaveDirectory") = 1
        .cOption("AutosaveDirectory") = sPDFPath
        .cOption("AutosaveFilename") = sPDFName
        .cOption("AutosaveFormat") = 0    ' 0 = PDF
        .cClearCache
    End With

    'Print documents to print queue in reverse order
    For lPrintOrder = Application.Documents.Count To 1 Step -1
        Application.Documents(lPrintOrder).PrintOut copies:=1
        lDocs = lDocs + 1
    Next lPrintOrder

    'Wait until all print jobs have entered the print queue
    Do Until pdfjob.cCountOfPrintjobs = lDocs
        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
    pdfjob.cClose
    Set pdfjob = Nothing
    Shell "taskkill /f /im PDFCreator.exe", vbHide
    On Error GoTo 0

    'Reset all application settings to user's original settings
    With Application
        .ScreenUpdating = True
        .ActivePrinter = sPrinter
        .Options.PrintBackground = bBkgrndPrnt
    End With
    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

Acknowledgments:

I also wanted to add a quick word of thanks to Word MVP Peter Jamieson for pointing me to the Background Printing resolution.

Share:

Facebook
Twitter
LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Posts