Search code examples
excelvbacom

Any way to determine when Excel background printing has finished?


I'm using COM in LotusScript (Lotus Notes) to make Excel print several sheets in one workbook to PDFCreator, then make PDFCreator combine then into one PDF. The problem is that calling Excel's PrintOut method immediately followed by PDFCreator's cCombineAll method results in one or more sheets being omitted from the PDF. It seems like Excel's PrintOut method returns before printing is complete.

Putting a Sleep in my code works, but may not be reliable as the printing time varies, so...
Is there any Excel property or method I can call to determine whether printing has finished?
Alternately, is there a way make the PrintOut method block until printing is finished?
I haven't been able to find an answer in Excel's VBA Help.


Solution

  • This sample code from excelguru.ca suggestst you need to use the cCountOfPrintJobs property of PDFCreator to monitor when the job starts and stops printing. Once printing is complete, you can perform whatever other operations you need

    Set pdfjob = New PDFCreator.clsPDFCreator
    
    ' ...missing out various initialisation steps
    
    '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