Results 1 to 3 of 3

Thread: PDFCreator

  1. #1

    PDFCreator



    Register for a FREE account, and/
    or Log in to avoid these ads!

    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

  2. #2
    Administrator Ken Puls's Avatar
    Join Date
    Mar 2011
    Location
    Nanaimo, BC, Canada
    Posts
    2,225
    Articles
    57
    Blog Entries
    14
    Excel Version
    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,
    Ken Puls, FCPA, FCMA, MS MVP (Excel)

    Learn to Master Your Data at the Power Query Academy (the world's most comprehensive online Power Query training) or with my book M is for Data Monkey!

    Main Site: http://www.excelguru.ca -||- Blog: http://www.excelguru.ca/blog -||- Forums: http://www.excelguru.ca/forums
    Check out the Excelguru Facebook Fan Page -||- Follow Me on Twitter

    If you've been given VBA code (a macro) for your solution, but don't know where to put it, CLICK HERE.

  3. #3
    I also tried, where is the problem?

Posting Permissions

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