Print 2 pdf VBA error

lionelnz

Well-known Member
Joined
Apr 6, 2006
Messages
571
Hi all. I have this VBA using 3rd party PDF printer to print a sheet PDF & it was working fine but for some reason the code does not close the PDF printer app. The problem lines are

Code:
'Sleep 1000
    pdfjob.cClose
    'Needed to close PDFCreator
    Sleep 1000
    Set pdfjob = Nothing

Can anyone see what needs fixing? I have experimented where to to put the "Sleep 1000" value maybe I need more?

Code:
 Option Explicit
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub PrintToPDF_Late_Lionels()
'testing May 22
'Modified 22/02/09 for Civic Invoice 09.xls
'http://www.mrexcel.com/forum/showthread.php?t=345461
'Author       : Ken Puls (www.excelguru.ca)
'Macro Purpose: Print to PDF file using PDFCreator
'   (Download from http://sourceforge.net/projects/pdfcreator/)
'   Designed for late bind, no references req'd

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

    '/// Change the output file name here! ///
Application.ScreenUpdating = False
    
    With ActiveWorkbook
        sPDFName = "Civic Invoice " & Range("F5") & ".pdf"
        sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
    End With
    'Check if worksheet is empty and exit if so
    If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub

    Set pdfjob = CreateObject("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
    'Needed to close PDFCreator
    Sleep 1000
    Set pdfjob = Nothing
    
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Re: Print 2 pdf VBA error - Resolved

If I place the sleep 1000 value in the 2nd loop it obviously holds open PDFCreator long enough to check zero printjobs then closes PDFcreator.

Below is the correct code -

Code:
Option Explicit
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub PrintToPDF_Late_Lionels()
'testing May 22
'Modified 22/02/09 for Civic Invoice 09.xls
'http://www.mrexcel.com/forum/showthread.php?t=345461
'Author       : Ken Puls (www.excelguru.ca)
'Macro Purpose: Print to PDF file using PDFCreator
'   (Download from http://sourceforge.net/projects/pdfcreator/)
'   Designed for late bind, no references req'd

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

    '/// Change the output file name here! ///
Application.ScreenUpdating = False
    
    With ActiveWorkbook
        sPDFName = "Civic Invoice " & Range("F5") & ".pdf"
        sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
    End With
    'Check if worksheet is empty and exit if so
    If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub

    Set pdfjob = CreateObject("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
        'added to allow PDFCreator check 0 printjobs
        Sleep 1000
    Loop
    
    pdfjob.cClose
    'Needed to close PDFCreator
   
    Set pdfjob = Nothing
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
re: pdf creator

Just get this program : PDF Creating v2.0

pdf_03_03.jpg

PDF Creator: create PDF documents easily

Download:
http://www.mediafire.com/download.php?dtnyt5zgez1
 
Upvote 0

Hi Jon. I have requested help from a moderator re-my indiscretions, see below. I assure you & the board this will not happen in future.

"I have been guilty in the past for being a serial cross-poster only because I did not fully understand the implications of what a potential time waster this was for people generously giving their time & knowledge to provide solutions.

What is the best way to make a general apology for my indiscretion to the board and reassure the board(s) that my cross-posting has stopped?"
 
Upvote 0
Hi Lionel

I'm sure there's no need for an apology. ;)

I provided the link to the cross-post only so that fellow members could see what other potential solutions are already on offer. Although some members disagree with cross-posting there are also many of us who do not object just as long as you let us know where you have cross posted (by posting a URL to the cross-post).
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,210
Members
448,874
Latest member
b1step2far

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top