Solved

how do I know if the printer is printing?

Posted on 1997-10-13
11
205 Views
Last Modified: 2012-05-04
can I, through VB, see if the printer currently is printing, and how many pages that have been printed out? thanx in advance!
0
Comment
Question by:davvan
  • 6
  • 5
11 Comments
 
LVL 5

Accepted Solution

by:
y96andha earned 200 total points
ID: 1437464
You can get the information you want through the EnumJobs function. Here is a sample showing the code that you need.

Private Const JOB_STATUS_DELETING = &H4
Private Const JOB_STATUS_ERROR = &H2
Private Const JOB_STATUS_OFFLINE = &H20
Private Const JOB_STATUS_PAPEROUT = &H40
Private Const JOB_STATUS_PAUSED = &H1
Private Const JOB_STATUS_PRINTED = &H80
Private Const JOB_STATUS_PRINTING = &H10
Private Const JOB_STATUS_SPOOLING = &H8
Private Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Byte, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, ByVal pDefault As Long) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long


Private Sub Command1_Click()
    Dim p As Long
    Dim needed As Long, returned As Long
    Dim pj(8192) As Byte
    If OpenPrinter(Printer.DeviceName, p, 0) Then
        If EnumJobs(p, 0, 100, 1, pj(0), 8192, needed, returned) Then
            For i = 0 To returned - 1
                Print "Status for job " & i & ": ";
                If (pj(i * 64 + 7 * 4) And JOB_STATUS_DELETING) <> 0 Then Print "Deleting ";
                If (pj(i * 64 + 7 * 4) And JOB_STATUS_ERROR) <> 0 Then Print "Error ";
                If (pj(i * 64 + 7 * 4) And JOB_STATUS_OFFLINE) <> 0 Then Print "Offline ";
                If (pj(i * 64 + 7 * 4) And JOB_STATUS_PAPEROUT) <> 0 Then Print "Paperout ";
                If (pj(i * 64 + 7 * 4) And JOB_STATUS_PAUSED) <> 0 Then Print "Paused ";
                If (pj(i * 64 + 7 * 4) And JOB_STATUS_PRINTED) <> 0 Then Print "Printed ";
                If (pj(i * 64 + 7 * 4) And JOB_STATUS_PRINTING) <> 0 Then Print "Printing ";
                If (pj(i * 64 + 7 * 4) And JOB_STATUS_SPOOLING) <> 0 Then Print "Spooling ";
                Print ", " & pj(i * 64 + 11 * 4) + pj(i * 64 + 11 * 4) * 256 & " pages printed"
            Next i
        End If
        ClosePrinter p
    Else
        Print "couldn't open printer"
    End If
End Sub


Please comment if you have any questions.

0
 
LVL 1

Author Comment

by:davvan
ID: 1437465
Thanx for the great and fast answer! But as I understand, this is VB4-code, and I would like to write the program in VB3. Do you know a way do do this?
0
 
LVL 5

Expert Comment

by:y96andha
ID: 1437466
This is VB5 code, but it should work in VB4 32-bit. It doesn't work in 16-bit anyway though.

I do not think that the 16-bit printer handling can access the number of pages printed. If you print yourself, you could perhaps make each page a different document? I don't have any 16-bit VB installed myself, so I am afraid I can't test it.

What operating system will you be using? It might be possible to check for the files of the print spooler to see when a job has finished printing.

0
 
LVL 1

Author Comment

by:davvan
ID: 1437467
I'll most use Windows NT, so I better write a 32-bit program ;-)
It should be great if the code worked on Windows 3.1x, but it doesn't matter.

Do you know if I can 'check for the files of the print spooler' in Windows NT?
0
 
LVL 5

Expert Comment

by:y96andha
ID: 1437468
In Windows NT, print jobs consist of two files stored in the %SystemRoot%\system32\spool\printers directory. It would be possible to check if the job has finished printing by checking for these files.

But if you do not need the 16-bit environment, I really would suggest moving to VB5. I know that VB4 is much slower than VB3, but VB5 is much faster than VB4, and little faster than VB3. And you could access all new functions.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:davvan
ID: 1437469
I saw in your user profile that you are from Sweden? Is that correct? I live in Gothenburg myself!
Right now, I only own the VB4 and I don't think I can afford to buy VB5, but I'll think about it.
Thanx about the tip of the printers directory. I'll check it out!
0
 
LVL 5

Expert Comment

by:y96andha
ID: 1437470
Sure, I am from Linkoping. I bought VB4 and MSVC++ 4.0 on student license in January for about 1000kr. A few months later I received free copies of VB5 and MSVC++ 5.0, as a free upgrade because they had been released when I bought the older versions.

What kind of program are you writing? VB4 32-bit isn't that bad on a fast computer.
0
 
LVL 1

Author Comment

by:davvan
ID: 1437471
We've got a small network with 5 users in it, and I am writing a program which logs how many pages each user are printing out. And thanx to you, I'm almost finished! Thank you a lot for helping me out!

We've got Pentium 133, so I think that's enough for VB4 32-bit
0
 
LVL 5

Expert Comment

by:y96andha
ID: 1437472
Suggestion: You could use event logging, and read the log so you don't have to keep your program running and checking the printer at all times. But it shouldn't be such a big performance issue anyway.
0
 
LVL 1

Author Comment

by:davvan
ID: 1437473
thanx for the great help you've been to me!
just one little questrion:
can I, through the log, see how many pages each user has printed out?
0
 
LVL 5

Expert Comment

by:y96andha
ID: 1437474
If you use NT Workstation and enable logging, I think so. And I am sure that you can if you are using NT Server. Of course this only works if the print job comes from a 32-bit client, as 16-bit clients don't pass information on the number of pages for a print job.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now