Solved

help with print job monitor

Posted on 2007-03-27
3
2,010 Views
Last Modified: 2013-11-07
I'm trying to create a program that when I run it I can capture a print jobs name,total pages, owner, etc.. I'm trying to use wmi win32_printjob for this, but I just can't seem to get it. I would like to be able to capture the info into variables so that I can write them to a file, database, etc... I found the below code and it works, I just can't figure out how to get it not to return so much info, just return the fields I want and how to get the info into variables for each property. Looking to get this working in vb.net

Thank you

Public Class Form1
    WithEvents watcher As ManagementEventWatcher

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) _
    Handles Me.FormClosed
        watcher.Stop()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load
        Dim q As New WqlEventQuery("__InstanceCreationEvent")
        q.WithinInterval = New TimeSpan(0, 0, 1)
        q.Condition = "TargetInstance ISA 'Win32_PrintJob'"
        watcher = New ManagementEventWatcher(q)
        watcher.Start()
    End Sub

    Private Sub watcher_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) _
    Handles watcher.EventArrived
        Dim printjob As ManagementBaseObject = e.NewEvent.Item("TargetInstance")
        Dim sb As New System.Text.StringBuilder
        sb.AppendLine("This printjob just started:")
        For Each p As PropertyData In printjob.Properties
            p.Name
            sb.AppendFormat("{0}: {1}", p.Name, p.Value)
            sb.AppendLine()


        Next
        MsgBox(sb.ToString)


    End Sub
End Class
0
Comment
Question by:heydude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
theGhost_k8 earned 500 total points
ID: 18805958
this guy helped me and hence it will work for u too : http://www.experts-exchange.com/M_38421.html

bob said:
I have this WMI class that retrieves PrintJob information, but I can't test it right now:

' Add a reference to System.Management.dll.

Imports System.Management

Namespace My.Computer.WMI

  Public Class PrintJob

    Public DataType As String
    Public Description As String
    Public Document As String
    Public DriverName As String
    Public ElapsedTime As Date
    Public HostPrintQueue As String
    Public InstallDate As Date
    Public JobId As Integer    'uint32
    Public JobStatus As String
    Public Notify As String
    Public Owner As String
    Public PagesPrinted As Integer     'uint32
    Public Parameters As String
    Public PrintProcessor As String
    Public Priority As Integer     'uint32
    Public Size As Integer     'uint32
    Public StartTime As Date
    Public Status As String
    Public StatusMask As Integer     'uint32
    Public TimeSubmitted As Date
    Public TotalPages As Integer     'uint32
    Public UntilTime As Date


    Public Shared ReadOnly Property List() As PrintJob()
      Get
        Dim query As String = "Select * From Win32_PrintJob"

        Dim searcher As New ManagementObjectSearcher(query)

        Dim results As ManagementObjectCollection = searcher.Get

        Dim listPrintJobs(results.Count - 1) As PrintJob

        Dim index As Integer = 0

        For Each entryCurrent As ManagementObject In results

          Dim job As New PrintJob

          job.DataType = entryCurrent("DataType")
          job.Description = entryCurrent("Description")
          job.Document = entryCurrent("Document")
          job.DriverName = entryCurrent("DriverName")
          job.ElapsedTime = entryCurrent("ElapsedTime")
          job.HostPrintQueue = entryCurrent("HostPrintQueue")
          job.InstallDate = entryCurrent("InstallDate")
          job.JobId = entryCurrent("JobId")
          job.JobStatus = entryCurrent("JobStatus")
          job.Notify = entryCurrent("Notify")
          job.Owner = entryCurrent("Owner")
          job.PagesPrinted = Convert.ToInt32(entryCurrent("PagesPrinted"))
          job.Parameters = entryCurrent("Parameters")
          job.PrintProcessor = entryCurrent("PrintProcessor")
          job.Priority = entryCurrent("Priority")
          job.Size = entryCurrent("Size")
          job.StartTime = entryCurrent("StartTime")
          job.Status = entryCurrent("Status")
          job.StatusMask = entryCurrent("StatusMask")
          job.TimeSubmitted = entryCurrent("TimeSubmitted")
          job.TotalPages = Convert.ToInt32(entryCurrent("TotalPages"))
          job.UntilTime = entryCurrent("UntilTime")

          listPrintJobs(index) = job

          index += 1

        Next entryCurrent

        Return listPrintJobs
      End Get
    End Property    'List

  End Class

End Namespace

Sample usage:
  Public Shared Function PrintJobs() As String

    Dim builder As New StringBuilder
    Dim list As PrintJob() = PrintJob.List()

    If list.Length = 0 Then
      builder.Append("(None)")
    Else
      For Each job As PrintJob In list
        builder.Append(job.Document & Environment.NewLine)
      Next
    End If

    Return builder.ToString()

  End Function
0
 

Author Comment

by:heydude
ID: 18808485
didn't really help me?
0
 

Author Comment

by:heydude
ID: 18833873
I did end up coming up with a way to do it. I'm going to give you the points because I can probably use the code above for something.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net Configuration, settings file location? 8 44
ModalPopup  question 22 40
locate sql commands in C# visual studio Project 6 45
VB.net Is there code behind PowerPivot 2 52
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

696 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