locked
HOW TO: Check printer spool

    Pregunta

  • Hi,

    I want to create a program that could check if the selected printer has a spool, what I mean it can check if the selected printer is currently printing or not.  It can check also the status, documents and other printer details.  Anybody here can help me pls

    Thanks...
    martes, 19 de agosto de 2008 2:16

Respuestas

  • Hi Cyber,

     

    Based on your post, my understanding of your question is that you need to monitor the printing queue and manipulate the printing jobs. In this scenario you can use Windows API to monitor a print queue and use WMI to control print jobs. I recommend that you read two articles about this kind of problem.

    1. Monitoring a Printer Queue from VB.NET. This article shows how to monitor a printer queue from Visual Basic .NET.
    2. A simple approach for controlling print jobs using WMI. This article presents a simplified approach of controlling print jobs using WMI.

    Or you can have a look at the PrintQueue class. Here is the code snippet about using this class.

     

    Imports System.Printing

    Imports System.Text

    Imports System.IO

    Public Class Form1

        Dim localPrintServer2 As LocalPrintServer

        Dim defaultPrintQueue2 As PrintQueue

        Dim ht1 As Hashtable

        Dim ht2 As Hashtable

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            localPrintServer2 = New LocalPrintServer()

            defaultPrintQueue2 = LocalPrintServer.GetDefaultPrintQueue()

            ht1 = New Hashtable

            ht2 = New Hashtable

            Timer1.Enabled = True

            Timer1.Interval = 50

            Dim jobs As PrintJobInfoCollection = defaultPrintQueue2.GetPrintJobInfoCollection

            For Each job As PrintSystemJobInfo In jobs

                ht2.Add(job.JobIdentifier, job.Name)

            Next

        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

            defaultPrintQueue2 = LocalPrintServer.GetDefaultPrintQueue()

            Dim jobs As PrintJobInfoCollection = defaultPrintQueue2.GetPrintJobInfoCollection

            For Each job As PrintSystemJobInfo In jobs

                If (Not ht2.Contains(job.JobIdentifier)) Then

                    If (Not ht1.Contains(job.JobIdentifier)) Then

                        ht1.Add(job.JobIdentifier, job.Name)

                        TextBox1.Text = TextBox1.Text & job.JobIdentifier.ToString & job.Name & vbNewLine

                        job.Pause()

                    End If

                End If

            Next

        End Sub

    End Class

    Best regards,

    Riquel

    miércoles, 20 de agosto de 2008 8:13
    Moderador

Todas las respuestas

  • Hi Cyber,

     

    Based on your post, my understanding of your question is that you need to monitor the printing queue and manipulate the printing jobs. In this scenario you can use Windows API to monitor a print queue and use WMI to control print jobs. I recommend that you read two articles about this kind of problem.

    1. Monitoring a Printer Queue from VB.NET. This article shows how to monitor a printer queue from Visual Basic .NET.
    2. A simple approach for controlling print jobs using WMI. This article presents a simplified approach of controlling print jobs using WMI.

    Or you can have a look at the PrintQueue class. Here is the code snippet about using this class.

     

    Imports System.Printing

    Imports System.Text

    Imports System.IO

    Public Class Form1

        Dim localPrintServer2 As LocalPrintServer

        Dim defaultPrintQueue2 As PrintQueue

        Dim ht1 As Hashtable

        Dim ht2 As Hashtable

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            localPrintServer2 = New LocalPrintServer()

            defaultPrintQueue2 = LocalPrintServer.GetDefaultPrintQueue()

            ht1 = New Hashtable

            ht2 = New Hashtable

            Timer1.Enabled = True

            Timer1.Interval = 50

            Dim jobs As PrintJobInfoCollection = defaultPrintQueue2.GetPrintJobInfoCollection

            For Each job As PrintSystemJobInfo In jobs

                ht2.Add(job.JobIdentifier, job.Name)

            Next

        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

            defaultPrintQueue2 = LocalPrintServer.GetDefaultPrintQueue()

            Dim jobs As PrintJobInfoCollection = defaultPrintQueue2.GetPrintJobInfoCollection

            For Each job As PrintSystemJobInfo In jobs

                If (Not ht2.Contains(job.JobIdentifier)) Then

                    If (Not ht1.Contains(job.JobIdentifier)) Then

                        ht1.Add(job.JobIdentifier, job.Name)

                        TextBox1.Text = TextBox1.Text & job.JobIdentifier.ToString & job.Name & vbNewLine

                        job.Pause()

                    End If

                End If

            Next

        End Sub

    End Class

    Best regards,

    Riquel

    miércoles, 20 de agosto de 2008 8:13
    Moderador
  • Hi Riquel,

    Thanks for the code, its working!, but is there any way to check if the printer was done printing?

    What I mean is when i embedded the above code in my program, it execute and display that the printer is printing but how do i know if it is done?

    Thanks again

    Regards,

    Lloyd

    martes, 16 de septiembre de 2008 7:18
  • Hi Lloyd,

    You can compare the current list to the previous list to know which job is finished in this scenario.

    Best regards,
    Riquel
    miércoles, 17 de septiembre de 2008 3:10
    Moderador
  • Hi Cyber,

    The snippet you provided will work perfectly in a scenario that I am setting up, the only problem is that it is only looking at the default printer's queue. I cant seem to figure out how to make it loop through all the printer queues and get the jobs from all printers installed on the machine. How could this code be modified to look at all printer queues on a system?

    Thanks a lot!
    miércoles, 05 de agosto de 2009 13:48
  • Hi,

    Thank you for this code...but I have a small problem.

    When the Job is paused we still can go in "Printers" (of the control panel) and right click on the job and resume it.
    How can we block that IN this program ?

    Something like :

    if(job.isresume)
    job.pause()
    end if

    ??
    Thank you !
    miércoles, 12 de agosto de 2009 1:27
  • hi riquel,

    Some how this code is not working for me. I have run the application and send prints to the
    default printer and its is not showing any thing in the text box and are not going in the loop at all

    For Each job As PrintSystemJobInfo In jobs

                If (Not ht2.Contains(job.JobIdentifier)) Then

                    If (Not ht1.Contains(job.JobIdentifier)) Then

                        ht1.Add(job.JobIdentifier, job.Name)

                        TextBox1.Text = TextBox1.Text & job.JobIdentifier.ToString & job.Name & vbNewLine

                        job.Pause()

                    End If

                End If

            Next


    I am new to vb.net and most probably its a small mistake

    sorry if its a stupid question


    miércoles, 21 de octubre de 2009 13:11
  • Hello Riquel,

     

    I found your code very helpfull and thanks for sharing, I just need some more assistance if you could help me, from this code I could gues your are expert in print programming and have good knowledge of it, any ways God bless your, next my first question is that, I would like to get the number of copies of a job, in the que and limit the users if they print more than selected copies the the queue should pause it. not the number of pages in a job, the number of copies, and the second thing is , this code works on the local machine, how if a print is sent from the network, is there any way that I can pause that print wich is coming from the network or another machine, on the network, most of my printers are connected through ip, and some are shared printers.

     

    I would really appreciate, your co operation in this regards,

     

    martes, 22 de noviembre de 2011 2:40