Determine number of jobs in advance


#1

I'm working on an Excel VBA implementation that allows users to select any number of sheets which the macro then outputs as a single password protect PDF. It is the ability to generate password protected PDFs programatically that led us to PDFCreator.

There is a problem - say the user selects three sheets that are portrait, landscape and portrait, three jobs are created when the following VBA is executed:

ActiveWindow.SelectedSheets.PrintOut

The solution would be to call WaitForJobs(NumJobs, WaitTime) but the rub is that I don't know in advance how many jobs there will be. Could be one, could be 10 is they picked sheets portrait, landscape, portrait etc.

I thought my solution might be WaitForJobs(MyQueue.Count, WaitTime) except that returns zero immediately. It counts up to (say) 3 if you keep checking it.

My workaround at the moment is a horrible sleep for 30 seconds which will probably work 99 times out of 100 but is hardly elegant.

My suspicion is that this is going to be a thorny problem due to the asynchronous nature of the Windows print system. The PDFCreator Queue can't look into the future to know now many print jobs are maybe still going to crop up. I changed the PDFCreator print queue to be offline and this is what happens when the above PrintOut is executed. Shows that three print jobs are created:


#2

I've found a reasonable solution for this specific instance. I do have a list of the Excel sheets that are going to be printed so I can walk through these looking at the page set-up to determine when the orientation changes, each time incrementing the job count. I can then use this with WaitForJobs.