PDFCreator

JPL007

New member
Joined
Oct 29, 2012
Messages
3
Reaction score
0
Points
0
Hi Ken
I tried your code, but it does not seem to work for me.
It does loop indefinitely.
Also the pdfcreator pop-up window, keeps on showing (blocking the automation of the process).
I am trying to do Word conversion to pdf (and cannot upgrade my office to a newer version than 2003, because I am at work).

Any help would be amazing.
thanks a lot
Julien



Code:
Sub GeneratePDF(ByVal sDocumentToConvert As String, ByVal sValue As String, ByVal sNewFolder As String)
    Dim pdfjob As PDFCreator.clsPDFCreator
    Dim p
    p = ActivePrinter
    WordApp.ActivePrinter = "PDFCreator"

    Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")

    With pdfjob
        .cOption("UseAutosave") = 1
        .cOption("UseAutosaveDirectory") = 1
        .cOption("AutosaveDirectory") = sNewFolder
        .cOption("AutosaveFilename") = sValue
        .cOption("AutosaveFormat") = 0 ' 0 = PDF
        .cClearCache
    End With
     'Print the document to PDF
    wrdDoc.PrintOut copies:=1
'     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
    ActivePrinter = p
End Sub
 

Ken Puls

Administrator
Staff member
Joined
Mar 13, 2011
Messages
2,519
Reaction score
4
Points
38
Location
Nanaimo, BC, Canada
Website
www.excelguru.ca
Excel Version(s)
Excel Office 365 Insider
Hi Julien,

I split this to a new thread for you.

The following code has a modification to the way that PDFCreator checks to seee if the file is complete, as well as a check to kill off PDFCreator if it's already running. (A very common cause of the hangup.) I've also included a test sub to make sure it fires:

Code:
Sub test()
    Call GeneratePDF(CStr(ActiveDocument.Name), "My PDF.pdf", ActiveDocument.Path & Application.PathSeparator)
End Sub
Sub GeneratePDF(ByVal sDocumentToConvert As String, ByVal sValue As String, ByVal sNewFolder As String)
    Dim pdfjob As Object
    Dim wrdDoc As Document
    Dim p
    Dim bRestart As Boolean
    
    'Bind to word documeent
    Set wrdDoc = Application.Documents(sDocumentToConvert)
    
    'Toggle printer settings
    p = Application.ActivePrinter
    Application.ActivePrinter = "PDFCreator"
   'Activate error handling and turn off screen updates
    On Error GoTo EarlyExit
    'Check if PDFCreator is already running and attempt to kill the process if so
    Do
        bRestart = False
        Set pdfjob = CreateObject("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
    With pdfjob
        .cOption("UseAutosave") = 1
        .cOption("UseAutosaveDirectory") = 1
        .cOption("AutosaveDirectory") = sNewFolder
        .cOption("AutosaveFilename") = sValue
        .cOption("AutosaveFormat") = 0 ' 0 = PDF
        .cClearCache
    End With
    
    'Remove any previous versions of the file
    On Error Resume Next
    Kill sNewFolder & sValue
    On Error GoTo EarlyExit
    
    'Print the document to PDF
    wrdDoc.PrintOut copies:=1
    
    '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
        DoEvents
    Loop Until Dir(sNewFolder & sValue) = sValue
Cleanup:
    'Release objects and terminate PDFCreator
    Application.ActivePrinter = p
    Set pdfjob = Nothing
    Shell "taskkill /f /im PDFCreator.exe", vbHide
    On Error GoTo 0
    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

Please note that I assumed the first variable you passed is the full file name, the second is the output filename (inlcuding .pdf) and the third is the file path (including the trailing \). If you need modifications, please let me know.

Hope it helps,
 
Top