How to check the printer job finished?
-
- PlatinumLounger
- Posts: 4576
- Joined: 26 Apr 2010, 17:36
How to check the printer job finished?
How to check the printer job finished?
-
- 5StarLounger
- Posts: 742
- Joined: 27 Jun 2021, 10:46
Re: How to check the printer job finished?
Are you wanting to know if a specific job (i.e one you have just submitted) is finished? Or whether any other jobs might be pending (e.g you are using a networked printer shared by multiple users)?
-
- 5StarLounger
- Posts: 742
- Joined: 27 Jun 2021, 10:46
Re: How to check the printer job finished?
Now I've planted that thought, here's some example code that covers both scenarios. You'll need a form with three command buttons
Code: Select all
Option Explicit
Public jobID As Long
' Is there a print job running on named printer
Private Sub Command1_Click()
Debug.Print IsPrintJobActive(Printer.DeviceName)
End Sub
Private Sub Command2_Click()
' send a print job to default printer
Printer.Print "Hello world"
Printer.EndDoc
'capture jobid
jobID = GetLastPrintJobID(Printer.DeviceName)
'immediatelt test for purposes of example whether job is running
Debug.Print IsSpecificPrintJobActive(jobID)
End Sub
Private Sub Command3_Click()
'check if nominated job is (still) running
Debug.Print IsSpecificPrintJobActive(jobID)
End Sub
Function IsPrintJobActive(printerName As String) As Boolean
Dim objWMI As Object
Dim objJob As Object
' Connect to WMI
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
For Each objJob In objWMI.ExecQuery("Select * From Win32_PrintJob")
If InStr(1, objJob.Name, printerName, vbTextCompare) > 0 Then
IsPrintJobActive = True
Exit For
End If
Next objJob
End Function
Private Function GetLastPrintJobID(ByVal printerName As String) As Long
Dim objWMI As Object
Dim objJob As Object
' Connect to WMI
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
' Loop through all print jobs on named printer
For Each objJob In objWMI.ExecQuery("Select * From Win32_PrintJob WHERE Name LIKE '" & printerName & "%'")
' Get the highest Job ID (latest job)
If objJob.jobID > GetLastPrintJobID Then
GetLastPrintJobID = objJob.jobID
End If
Next objJob
End Function
Private Function IsSpecificPrintJobActive(ByVal jobID As Long) As Boolean
Dim objWMI As Object
Dim objJob As Object
' Connect to WMI
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
' Check for the specific job id
For Each objJob In objWMI.ExecQuery("Select * From Win32_PrintJob WHERE JobId = " & jobID)
IsSpecificPrintJobActive = True
Exit For
Next objJob
End Function
-
- PlatinumLounger
- Posts: 4576
- Joined: 26 Apr 2010, 17:36
Re: How to check the printer job finished?
i test tomorrow!SpeakEasy wrote: ↑05 Feb 2025, 13:59Now I've planted that thought, here's some example code that covers both scenarios. You'll need a form with three command buttons
Code: Select all
Option Explicit Public jobID As Long ' Is there a print job running on named printer Private Sub Command1_Click() Debug.Print IsPrintJobActive(Printer.DeviceName) End Sub Private Sub Command2_Click() ' send a print job to default printer Printer.Print "Hello world" Printer.EndDoc 'capture jobid jobID = GetLastPrintJobID(Printer.DeviceName) 'immediatelt test for purposes of example whether job is running Debug.Print IsSpecificPrintJobActive(jobID) End Sub Private Sub Command3_Click() 'check if nominated job is (still) running Debug.Print IsSpecificPrintJobActive(jobID) End Sub Function IsPrintJobActive(printerName As String) As Boolean Dim objWMI As Object Dim objJob As Object ' Connect to WMI Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") For Each objJob In objWMI.ExecQuery("Select * From Win32_PrintJob") If InStr(1, objJob.Name, printerName, vbTextCompare) > 0 Then IsPrintJobActive = True Exit For End If Next objJob End Function Private Function GetLastPrintJobID(ByVal printerName As String) As Long Dim objWMI As Object Dim objJob As Object ' Connect to WMI Set objWMI = GetObject("winmgmts:\\.\root\cimv2") ' Loop through all print jobs on named printer For Each objJob In objWMI.ExecQuery("Select * From Win32_PrintJob WHERE Name LIKE '" & printerName & "%'") ' Get the highest Job ID (latest job) If objJob.jobID > GetLastPrintJobID Then GetLastPrintJobID = objJob.jobID End If Next objJob End Function Private Function IsSpecificPrintJobActive(ByVal jobID As Long) As Boolean Dim objWMI As Object Dim objJob As Object ' Connect to WMI Set objWMI = GetObject("winmgmts:\\.\root\cimv2") ' Check for the specific job id For Each objJob In objWMI.ExecQuery("Select * From Win32_PrintJob WHERE JobId = " & jobID) IsSpecificPrintJobActive = True Exit For Next objJob End Function
Tks for code
-
- Administrator
- Posts: 12949
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: How to check the printer job finished?
Many printers will continue printing the document for a long time after the print job finishes. That typically just shows that the printer has received the document
StuartR
-
- 5StarLounger
- Posts: 742
- Joined: 27 Jun 2021, 10:46
Re: How to check the printer job finished?
That's the case for very few printers these days. A job remains in a printers print queue until the printer signals it has successfully printed (not just received and buffered) all the pages, or if the job is cancelled. This uses the bidirectional communication most printers support (also used to signal ink levels, for example), even cheap ones, a feature introduced with XP.
-
- Administrator
- Posts: 12949
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe