How do I get the total number of pages in a print job from Win32_PrintJob, including multiple copies?

I'm writing a Printer manager for my Internet cafe management software but unfortunately I've run into a brick wall.

Everything is working so far, my printer watch software written in Visual Basic happily picks up print jobs from the paused print queue. I can find the total number of pages of the document printed for everything I've tested so far. However, if I chose to print multiple copies of the same page, this seems to come up in the print job as only a single copy.

So for example if I type out a single line in notepad and print it, it will come out as 1 page on the print job and print out 1 page from the printer.

If I print out the same single line from notepad but set 'Copies' to 2, it still appears to be 1 page on the print job, but 2 pages will be printed out from the printer.

This is no good for me because I am still charging for only a single print when 2 copies have been printed!

My print watch software keeps the printer queue paused so I can't use 'PagesPrinted' even if it did provide the currect number of total pages printed.

Is there any way to find out how many copies were printed on a print job, as then I could just multiply 'TotalPages' by the number of copies to work out how many total pages there actually is.

The bit of code I'm using to look at print jobs is this:

Set colPrintJobs = objWMIService.ExecQuery ("Select * from Win32_PrintJob")
For Each objPrintJob in colPrintJobs
msgbox objPrintJob.TotalPages
Next objPrintJob

If I print out a single page document with copies set to 10, I want to know that 10 pages have been printed in total. Right now all I know is that a single page document has been printed but I can't see how many copies have been set.

None of the other columns on the Win32_PrintJob seem to show either the actual total number of pages or the number of copies -
Who is Participating?
MerrionConnect With a Mentor Commented:
(Yes - I am the developer behind the Merrion Computing products.)  

The print job copies returned in that topic is the same as you are getting but there is an asynchronous method "ParseSpoolfile" that parses the spoolfile and raises an "OnSpoolfileParsed" event that has the number of copies and pages etc.

This communicates with a service that runs on the print server and watches the spool file directory and takes a note of every new file that arrives.  It then parses the spool header file (*.shd) to find out what kind of data the spool file itself (*.spl) contains...the format of this header file is undocumented but I have reverse engineered it here:

Once you find out if it is a "RAW" file or an "EMF" file you need to parse it.  For RAW files you need to find out if they are PostScript, PCL, HPGL etc. as each is encoded differently.  For EMF files the format is simpler (but again undocumented).  I'll see if I have the formats to hand...


Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

HASujoyAuthor Commented:
Thanks for the links I'll try out the code in there. I hope that 'TotalPages' that these snippets provide is actually correct rather than the incorrect value I'm getting from Win32_PrintJob.
The only way to get the true number of copies is to parse the print spool file.  This is bceasue some applications actually implement their own multi copy handling (especially MS Word) and therefore have to effectively lie to the print spooler...

I have written spool file parsing code for PCL, PostScript and EMF but they are part of a commercial product so I can't give you the code I'm afraid.
HASujoyAuthor Commented:
Is that Merrion from Because I read through the whole website when I was working out ways to capture printing information, a huge thankyou for all the effort you put in (if that's you!).

As for getting the true number of copies, I was crossing my fingers hoping it wasn't going to be a complex process. Life would be so much easier otherwise! Parsing the spool file would be over the top for my 'simple' program even if you could give me some pointers.

It's a bit of a shame because I've already written the print manager client and it works perfectly for reporting information back to my server, that then figures out who initiated the printing and triggers a question on their desktop asking how they want to pay - either from their credit or cash at the counter. It would save a huge amount of staff time at my Internet cafes. Without being able to accurately determine how many pages have been sent to print in a simple way it looks like I won't be able to automatically charge for printing.

If there is any other way you can think of to determine the number of pages being printed on a paused print queue It'd save my printer management module! I don't suppose 'PagesPrinted' on Win32_PrintJob is any different from 'TotalPages' - I noticed that it's 0 before printing has started, so I assume it just counts through as pages go to the Printer.
HASujoyAuthor Commented:
After I wrote that I just thought I'd rebrowse the site. I found this page, will it work for finding the actual number of copies?
HASujoyAuthor Commented:
Ah I notice that it is a commercial product, however if it solves my problems that'd work for me.
HASujoyAuthor Commented:
Duncan I don't suppose you could confirm whether or not the PrintQueueWatch component on your website returns the accurate number of copies? I tried to email but the address from your website (from the licensing link) doesn't seem to be up-to-date!
HASujoy Doesn't the link I provide tell you how many pages are being printed. I have tested it and I believe it does.
I am putting together a trial version of the latest component which _does_ return the correct number of copies, when you call ParseSpoolfile for the job.  

There is a write up of the EMF spoolfile format that I have put up here:

HASujoyAuthor Commented:
Hi egl1044,

It's a bit lax of me but I didn't try that piece of code after reading Merrion's information. However to be thorough I'll try now and respond. Thanks.

HASujoyAuthor Commented:
Ok egl1044, I just tried that example and exactly the same thing happens as with the code I pasted above. The number of pages reported is always wrong when you have multiple copies. If I write a single line in notepad and print a hundred copies, it is reported as a single page printed. This is no use to me unfortunately and is the basis of my original question.

Did you have a different experience with the code on that Microsoft page? Try printing multiple copies of anything and see if the total number of pages reported is correct?
HASujoyAuthor Commented:
Merrion it sounds like the best thing for me to do is standby for your component. I'll check your blog updates for more info. If you release a component that can capture print jobs correctly I, and I'm sure many others, will be happy to pay a (small) license fee to use it!
Dear HASujoy,

I have had the same problem for years now. My code counts pages and not copies. Unfortunately I don't seem to understand the accepted solution above. Kindly help me with the printing code so that I can have an accurate solution. I also have a cyber software and I still program in VB 6.

Your help will be really appreciated.


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.