How to check the printer job finished?

User avatar
sal21
PlatinumLounger
Posts: 4576
Joined: 26 Apr 2010, 17:36

How to check the printer job finished?

Post by sal21 »

How to check the printer job finished?

User avatar
SpeakEasy
5StarLounger
Posts: 742
Joined: 27 Jun 2021, 10:46

Re: How to check the printer job finished?

Post by SpeakEasy »

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)?

User avatar
SpeakEasy
5StarLounger
Posts: 742
Joined: 27 Jun 2021, 10:46

Re: How to check the printer job finished?

Post by SpeakEasy »

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

User avatar
sal21
PlatinumLounger
Posts: 4576
Joined: 26 Apr 2010, 17:36

Re: How to check the printer job finished?

Post by sal21 »

SpeakEasy wrote:
05 Feb 2025, 13:59
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
i test tomorrow!
Tks for code

User avatar
StuartR
Administrator
Posts: 12949
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: How to check the printer job finished?

Post by StuartR »

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


User avatar
SpeakEasy
5StarLounger
Posts: 742
Joined: 27 Jun 2021, 10:46

Re: How to check the printer job finished?

Post by SpeakEasy »

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.

User avatar
StuartR
Administrator
Posts: 12949
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: How to check the printer job finished?

Post by StuartR »

:thumbup:
StuartR