Code Samples for Businesses, Schools & Developers

First Published 19 July 2023                   Last Updated 27 July 2023

I received an email from Lorenzo Garuglieri asking if I knew how to determine the number of jobs in the print queue.
This was so he could determine whether the print queue was empty or not.

PrintQueueDialog
Prior to contacting me, Lorenzo had found two useful articles:

1.   Code at Daniel Pineault's DevHut website which gave information about default printers (but not the print queue):

2.   Old code at BinaryWorld but it was only for 32bit, so he couldn't verify it

      I tested the code in the second link and it works for 32-bit Access giving output similar to this:

TestCode ExampleOutput1


However the original code is very long and would need several changes to work in 64-bit Access

I had never needed this functionality but thought it would be easy enough to create a solution using Windows Machine Instrumentation (WMI) using code similar to that in my Detailed System Info example app

WMI is a very powerful tool that can provide a huge amount of information about your computer including attached hardware and installed software.

By interrogating the Win32_Printers WMI, I easily found the name of the default printer and the number of jobs outstanding.
To do so, I adapted the output from the example database in my article Detailed System Info

I modified the GetSysInfo function in module modSysInfo by first enabling the following line:

WMI(15) = "WIn32_Printer"       'Installed Printers in sub GetSysInfo in module modSysInfo


I ran the code & searched the output table tblSysInfo that it populates
I filtered the output for wmi = Win32_Printer* and then for the printer listed as Default = True
From there, I checked the value for the property JobCountSinceLastReset

SysInfo
This gave me all the information I needed to write a convenience function to give this information direct.
In less than 15 minutes, I had written the following function. This requires the reference Microsoft WMI Scripting 1.2 Library

CODE:

Function GetDefaultPrinterQueueJobs()

'==================================================================
'Author:                    Colin Riddington
'Company:               Mendip Data Systems
'Website:                  https://isladogs.co.uk
'Purpose:                  Gets the number of jobs in the print queue for the default printer
'Last Updated:        19/07/2023
'Reference:              Microsoft WMI Scripting 1.2 Library
'==================================================================

'Requires the reference - Microsoft WMI Scripting 1.2 Library

Dim strComputer As String
Dim objWMIService As Object, objPrinter As Object
Dim colSettings As Variant

strComputer = "."       'local host - modify for a network printer
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery("Select * from Win32_Printer WHERE Default = True")

For Each objPrinter In colSettings
      'print output to immediate window
      Debug.Print "Default Printer: " & objPrinter.DeviceID
      Debug.Print "Print Jobs in Queue: " & objPrinter.JobCountSinceLastReset

      GetDefaultPrinterQueueJobs = objPrinter.JobCountSinceLastReset

Next

End Function



Example output:

ExampleOutput2

If preferred, replace the 2 Debug.Print lines with a message box (as above) displaying the number of outstanding jobs in the print queue.

One possible use would be to ask users to confirm whether they want to proceed with printing a report depending on the number of jobs waiting to be processed.
For example:

Print Jobs Waiting Message
CODE:

Private Sub cmdPrint_Click()

      'Inform user if there are print jobs waiting

      Select Case GetDefaultPrinterQueueJobs

      Case Is > 1
      If MsgBox("There are already " & GetDefaultPrinterQueueJobs & " jobs waiting in the print queue. " & vbCrLf & _
            "Do you want to continue?", vbYesNo + vbQuestion, "Print Queue information") = vbNo Then Exit Sub

      Case 1
     If MsgBox("There is already " & GetDefaultPrinterQueueJobs & " job waiting in the print queue. " & vbCrLf & _& _
            "Do you want to continue?", vbYesNo + vbQuestion, "Print Queue information") = vbNo Then Exit Sub

      Case Else
      'no jobs waiting - proceed

      End Select

      'print the report
      DoCmd.OpenReport "rptSysInfo", acViewNormal       'acViewPreview

End Sub





A minor change to the code will allow you to check the print queue for all installed printers

CODE:

Function GetAllPrinterQueueJobs()

'==================================================================
'Author:                    Colin Riddington
'Company:               Mendip Data Systems
'Website:                  https://isladogs.co.uk
'Purpose:                  Gets the number of jobs in the print queue for all installed printers
'Last Updated:        23/07/2023
'Reference:              Microsoft WMI Scripting 1.2 Library
'==================================================================

'Requires the reference - Microsoft WMI Scripting 1.2 Library

Dim strComputer As String
Dim objWMIService As Object, objPrinter As Object
Dim colSettings As Variant, i As Integer

strComputer = "."       'local host - modify for a network printer
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery("Select * from Win32_Printer")

i = 0

Debug.Print "Printer Queue" & vbCrLf & "============" & vbCrLf

For Each objPrinter In colSettings
      'print output to immediate window for all installed printers
      If objPrinter.Default = True Then
            Debug.Print "Printer: " & objPrinter.DeviceID & " (Default)"
      Else
            Debug.Print "Printer: " & objPrinter.DeviceID
      End If

      Debug.Print "Print Jobs in Queue: " & objPrinter.JobCountSinceLastReset

      i = i + objPrinter.JobCountSinceLastReset

Next

GetAllPrinterQueueJobs = i
Debug.Print vbCrLf & "Total number of jobs in print queue = " & i

End Function



Example output:

ExampleOutput3



The day after posting the second section of code, I received an email from Steve Schechner who suggested adding an optional filter allowing users to only show details for printers with jobs in the print queue. This seemed an excellent idea and the following shows one way it can be done

CODE:

Function GetAllPrintersWithQueueJobs(Optional hasJobs As Boolean = False)

'==================================================================
'Author:                    Colin Riddington
'Company:               Mendip Data Systems
'Website:                  https://isladogs.co.uk
'Purpose:                  Gets the number of jobs in the print queue for all installed printers
'                                  Optional filter for those with print jobs only
'Last Updated:        27/07/2023
'Reference:              Microsoft WMI Scripting 1.2 Library
'==================================================================

'Requires the reference - Microsoft WMI Scripting 1.2 Library

Dim strComputer As String
Dim objWMIService As Object, objPrinter As Object
Dim colSettings As Variant, i As Integer


strComputer = "."       'local host - modify for a network printer
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery("Select * from Win32_Printer")

i = 0

Debug.Print "Printer Queue" & vbCrLf & "============" & vbCrLf

For Each objPrinter In colSettings
      If hasJobs = False (or is omitted), Abs(hasJobs) = 0 so all printers are included
      If hasJobs = True (-1), Abs(hasJobs) = 1 so only printers with jobs are listed
      If objPrinter.JobCountSinceLastReset >= Abs(hasJobs)
           'print output to immediate window
            If objPrinter.Default = True Then
                  Debug.Print "Printer: " & objPrinter.DeviceID & " (Default)"
            Else
                  Debug.Print "Printer: " & objPrinter.DeviceID
            End If

            Debug.Print "Print Jobs in Queue: " & objPrinter.JobCountSinceLastReset

            i = i + objPrinter.JobCountSinceLastReset
      End If

Next

GetAllPrintersWithQueueJobs = i
Debug.Print vbCrLf & "Total number of jobs in print queue = " & i

End Function



I hope this information will be useful to others



Download

Click to download all the above code as a .bas (text) file for importing into your own applications as a standard module:

        modPrinter             .bas file     (zipped)



Feedback

Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.

Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington           Mendip Data Systems                 Last Updated 27 July 2023



Return to Code Samples Page




Return to Top