Consulting

Results 1 to 6 of 6

Thread: Print Word document to PDFcreator

  1. #1

    Print Word document to PDFcreator

    Hi,

    I'm trying to print an opened Word document to PDFcreator automatically using VBA. I thought I could adapt some code I found on Excelguru.ca (see below). This code uses early binding and requires me to set a reference to PDFcreator, but I can't find this in the list of references even though PDFCreator is running on my machine. Can anyone help?

    Option Explicit

    Sub PrintToPDF_Early()
    'Author : Ken Puls ()
    'Macro Purpose: Print to PDF file using PDFCreator

    ' Designed for early bind, set reference to PDFCreator

    Dim pdfjob As PDFCreator.clsPDFCreator
    Dim sPDFName As String
    Dim sPDFPath As String

    '/// Change the output file name here! ///
    sPDFName = "testPDF.pdf"
    sPDFPath = ActiveWorkbook.Path & Application.PathSeparator

    'Check if worksheet is empty and exit if so
    If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub

    Set pdfjob = New PDFCreator.clsPDFCreator

    With pdfjob
    If .cStart("/NoProcessingAtStartup") = False Then
    MsgBox "Can't initialize PDFCreator.", vbCritical + _
    vbOKOnly, "PrtPDFCreator"
    Exit Sub
    End If
    .cOption("UseAutosave") = 1
    .cOption("UseAutosaveDirectory") = 1
    .cOption("AutosaveDirectory") = sPDFPath
    .cOption("AutosaveFilename") = sPDFName
    .cOption("AutosaveFormat") = 0 ' 0 = PDF
    .cClearCache
    End With

    'Print the document to PDF
    ActiveSheet.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 PDF creator is finished then release the objects
    Do Until pdfjob.cCountOfPrintjobs = 0
    DoEvents
    Loop
    pdfjob.cClose
    Set pdfjob = Nothing


    End Sub

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Why not
    [VBA]Sub PdfPrint()
    Dim p As String
    p = ActivePrinter
    ActivePrinter = "PDFCreator"
    ActiveDocument.PrintOut
    ActivePrinter = p
    End Sub[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Thanks, but it's not as simple as that.
    I want to make sure that the PDFcreator settings are set to Autosave and I want to add a filename and destination. I've tried doing this with Sendkeys but I can't seem to get the sendkeys to go to the PDFcreator dialog.

  4. #4
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Quote Originally Posted by A. Vervoort
    Thanks, but it's not as simple as that.
    I want to make sure that the PDFcreator settings are set to Autosave and I want to add a filename and destination. I've tried doing this with Sendkeys but I can't seem to get the sendkeys to go to the PDFcreator dialog.
    I use this adapted routine (originally from Ken Puls site) for myself (ps.: you are messing with coding for excel so that won't work in Word):
    [vba]Sub PrintToPDF_ActiveWordDoc_to_SingleFile()
    'Author : Charlize (original coding from Ken Puls)
    'Macro Purpose: Print active Word documents to PDF (filename of document)
    '
    ' (Download from http://sourceforge.net/projects/pdfcreator/)
    ' Designed for early bind, set reference to PDFCreator
    ' Extra - References (verwijzingen)
    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
    Dim mytime As Long

    '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 = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 4)
    'InputBox("Geef naam :", "Geef naam van bestand ...")
    'sPDFName = "Consolidated.pdf"
    sPDFPath = ActiveDocument.Path & Application.PathSeparator '"C:\Temp\"
    If sPDFPath = "\" Or InStr(1, sPDFPath, "TMP", vbTextCompare) > 1 Then
    MsgBox "Gelieve eerst uw Word document te bewaren alvorens" & vbCrLf & _
    "om te willen zetten naar een pdf file.", vbInformation
    Exit Sub
    End If
    '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
    Application.ActiveDocument.PrintOut copies:=1
    '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
    mytime = 1
    Do While mytime < 500
    mytime = mytime + 1
    Loop

    MsgBox "Uw document werd naar een pdf omgezet.", vbInformation, 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 "Er werd een probleem vastgesteld. PDFCreator werd" & vbCrLf & _
    "vroegtijdig be?indigd. Probeer aub nogmaals en/of" & vbCrLf & _
    "contacteer de helpdesk.", _
    vbCritical + vbOKOnly, "Error"
    Resume Cleanup
    End Sub[/vba]Charlize
    Last edited by Charlize; 01-21-2009 at 03:48 AM.

  5. #5
    Why dont you use Save As PDF add in from Microsoft. You can just save your document as PDF without having to print it to a PDF Printer.

  6. #6
    As far as I know that only works in Office 2007. I want to print to PDF in Office 2003

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •