Solved

help with print job monitor

Posted on 2007-03-27
3
1,998 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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