Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Detect Windows Printer Status Using Visual Basic

Posted on 2005-05-09
Medium Priority
Last Modified: 2013-11-25
I have an Access 2k / VB application that loops through a list of customers and prints to WinFax Fax Driver (basically another printer). I am looking for some VB code that can basically check the status of this printer and if Spooling or Printing then suspend the VB application until finished.  There should only be one print job in the print que.  Once finished, the VB application will send the next print job until it cycles through the entire recordset based on customer number.

Kind of like ...  

While WinFax Printer Status = "Spooling" OR WinFax Printer Status = "Printing"
  Do Somtheing

Any help will be appreciated.


Question by:Eric Sherman
  • 2
LVL 22

Accepted Solution

JesterToo earned 1500 total points
ID: 13965291
I believe you will find it necessary to use the Windows API to obtain what you need... the FSO printer object doesn't break down the printer status codes in a very useful manner.  See this link for a pretty complete article and code on obtaining Printer status codes that you can use...


LVL 19

Author Comment

by:Eric Sherman
ID: 13965616
Thanks Jester for the responses.  Looks like the GetPrinter API should be able to accomplish what I am trying to do.  I reviewed the link you provided and I'm not that familiar with the GetPrinter API so I need a little help to get started.

I have a printer named "WinFax (Photo Quality)".   All I need to find out is if the JobsCount for this printer is greater than 1 then I will supspend the application running in VB.  In other words ...

While "WinFax (Photo Quality)" JobsCount > 1 Then
  Do Something

I need help with the structure of the code to reference the GetPrinter API in order to read the JobsCount for the printer.
I will probably have the declare the following fuction but where/how do you reference the printer you are trying to get the JobsCount for???

Private Declare Function GetPrinterApi Lib "winspool.drv" Alias _
       "GetPrinterA" (ByVal hPrinter As Long, _
         ByVal Level As Long, _
         buffer As Long, _
         ByVal pbSize As Long, _
         pbSizeNeeded As Long) As Long

Private Type PRINTER_INFO_2
   JobsCount As Long



LVL 19

Author Comment

by:Eric Sherman
ID: 13972839
Ok, I searched around and put this together from a couple of other posts.  This seems to work and it find out how many print jobs are in the printer que.  I have a question if anyone can answer, please.  In the Function below HowMany() the following code is included and I'm not sure how to understand what it's doing.

    If pcbNeed > 0 Then
        ReDim pJob(pcbNeed - 1)
        retval = EnumJobs(hPrinter, 0, 99, 1, pJob(0), pcbNeed, pcbNeed, pcRet)
    End If

When I tested the contents of the variables it would look something like this ... I'm not sure how the ReDim pJob got to 66.

    If 176 > 0 Then
        ReDim pJob(176 - 1)
        retval = EnumJobs(155667028, 0, 99, 1, 66, 176, 176, 1)
    End If

Public Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" _
    (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long

Public 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 Any, ByVal cdBuf As Long, _
    pcbNeed As Long, pcReturned As Long) As Long

Public Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long

Function HowMany()
Dim hPrinter As Long
Dim pcbNeed As Long, pcRet As Long
Dim pJob() As Byte
Dim lngJobsCount As Long
Dim retval As Long
Dim strPrinter As String

    'Set your printer name here
    strPrinter = "WinFax (photo quality)"
    retval = OpenPrinter(strPrinter, hPrinter, ByVal vbNullString)
    retval = EnumJobs(hPrinter, 0, 99, 1, ByVal vbNullString, 0, pcbNeed, pcRet)
    If pcbNeed > 0 Then
        ReDim pJob(pcbNeed - 1)
        retval = EnumJobs(hPrinter, 0, 99, 1, pJob(0), pcbNeed, pcbNeed, pcRet)
    End If
    retval = ClosePrinter(hPrinter)
    HowMany = pcRet
    MsgBox pcbNeed - 1
End Function

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

581 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