Jump to content

Check Spooler PrintJob


Shibuya
 Share

Recommended Posts

Got this command from Scriptomatic

snipped a little to get what I want

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output= ""
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PrintJob", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) Then
    For $objItem In $colItems
        $Output = $Output & "Document: " & $objItem.Document & @CRLF
        
        If Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
        $Output=""
    Next
    
Else
    Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_PrintJob" )

EndIf

The code works when something's in the spooler

but doesn't display the messagebox when the spooler's empty

My purpose for this code is to :

Display what's inside the spooler if it's not empty

if it's empty, prompt or return me something

Anyone who's familiar with this please help?

:P

Edited by Shibuya

The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

Link to comment
Share on other sites

im not sure if this can help! but im trying! this is a VB script i found on the net to check printer!

before i send you this code! just to let you know! in windows platforme any version! you got this dir:

C:\WINDOWS\system32\spool\PRINTERS everyfile in there it printing job file waiting for print!

you can check the file to know if a print job is waiting!

FP00001.SHD: is the printingjob definition this file is locked by the print processor

FP00001.SPL: and this is the data to print

if you have no file in there! there is no print job waiting! if there file for each pair of file like this up you have 1 print job!

ok here is the VB code

Public Function CheckPrinter(PrinterStr As String, JobStr As String) As String
   Dim hPrinter As Long
   Dim ByteBuf As Long
   Dim BytesNeeded As Long
   Dim PI2 As PRINTER_INFO_2
   Dim JI2 As JOB_INFO_2
   Dim PrinterInfo() As Byte
   Dim JobInfo() As Byte
   Dim result As Long
   Dim LastError As Long
   Dim PrinterName As String
   Dim tempStr As String
   Dim NumJI2 As Long
   Dim pDefaults As PRINTER_DEFAULTS
   Dim i As Integer
   
   'Set a default return value if no errors occur.
   CheckPrinter = "Printer info retrieved"
   
   'NOTE: You can pick a printer from the Printers Collection
   'or use the EnumPrinters() API to select a printer name.
   
   'Use the default printer of Printers collection.
   'This is typically, but not always, the system default printer.
   PrinterName = Printer.DeviceName
   
   'Set desired access security setting.
   pDefaults.DesiredAccess = PRINTER_ACCESS_USE
   
   'Call API to get a handle to the printer.
   result = OpenPrinter(PrinterName, hPrinter, pDefaults)
   If result = 0 Then
      'If an error occurred, display an error and exit sub.
      CheckPrinter = "Cannot open printer " & PrinterName & _
         ", Error: " & Err.LastDllError
      Exit Function
   End If

   'Init BytesNeeded
   BytesNeeded = 0

   'Clear the error object of any errors.
   Err.Clear

   'Determine the buffer size that is needed to get printer info.
   result = GetPrinter(hPrinter, 2, 0&, 0&, BytesNeeded)
   
   'Check for error calling GetPrinter.
   If Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER Then
      'Display an error message, close printer, and exit sub.
      CheckPrinter = " > GetPrinter Failed on initial call! <"
      ClosePrinter hPrinter
      Exit Function
   End If
   
   'Note that in Charles Petzold's book "Programming Windows 95," he
   'states that because of a problem with GetPrinter on Windows 95 only, you
   'must allocate a buffer as much as three times larger than the value
   'returned by the initial call to GetPrinter. This is not done here.
   ReDim PrinterInfo(1 To BytesNeeded)
   
   ByteBuf = BytesNeeded
   
   'Call GetPrinter to get the status.
   result = GetPrinter(hPrinter, 2, PrinterInfo(1), ByteBuf, _
     BytesNeeded)
   
   'Check for errors.
   If result = 0 Then
      'Determine the error that occurred.
      LastError = Err.LastDllError()
      
      'Display error message, close printer, and exit sub.
      CheckPrinter = "Couldn't get Printer Status!  Error = " _
         & LastError
      ClosePrinter hPrinter
      Exit Function
   End If

   'Copy contents of printer status byte array into a
   'PRINTER_INFO_2 structure to separate the individual elements.
   CopyMemory PI2, PrinterInfo(1), Len(PI2)
   
   'Check if printer is in ready state.
   PrinterStr = CheckPrinterStatus(PI2.Status)
   
  'bala 'Add printer name, driver, and port to list.
  'bala 'PrinterStr = PrinterStr & "Printer Name = " & _
   'bala ' GetString(PI2.pPrinterName) & vbCrLf
  'bala 'PrinterStr = PrinterStr & "Printer Driver Name = " & _
  'bala  ' GetString(PI2.pDriverName) & vbCrLf
   'bala'PrinterStr = PrinterStr & "Printer Port Name = " & _
   'bala ' GetString(PI2.pPortName) & vbCrLf
   
   'Call API to get size of buffer that is needed.
   result = EnumJobs(hPrinter, 0&, &HFFFFFFFF, 2, ByVal 0&, 0&, _
      BytesNeeded, NumJI2)
   
   'Check if there are no current jobs, and then display appropriate message.
   If BytesNeeded = 0 Then
      JobStr = "Ready"
   Else
      'Redim byte array to hold info about print job.
      ReDim JobInfo(0 To BytesNeeded)
      
      'Call API to get print job info.
      result = EnumJobs(hPrinter, 0&, &HFFFFFFFF, 2, JobInfo(0), _
        BytesNeeded, ByteBuf, NumJI2)
      
      'Check for errors.
      If result = 0 Then
         'Get and display error, close printer, and exit sub.
         LastError = Err.LastDllError
         CheckPrinter = " > EnumJobs Failed on second call! <  Error = " _
            & LastError
         ClosePrinter hPrinter
         Exit Function
      End If
      
      'Copy contents of print job info byte array into a
      'JOB_INFO_2 structure to separate the individual elements.
      For i = 0 To NumJI2 - 1   ' Loop through jobs and walk the buffer
          CopyMemory JI2, JobInfo(i * Len(JI2)), Len(JI2)
             
          ' List info available on Jobs.
          Debug.Print "Job ID" & vbTab & JI2.JobId
          Debug.Print "Name Of Printer" & vbTab & _
            GetString(JI2.pPrinterName)
          Debug.Print "Name Of Machine That Created Job" & vbTab & _
            GetString(JI2.pMachineName)
          Debug.Print "Print Job Owner's Name" & vbTab & _
            GetString(JI2.pUserName)
          Debug.Print "Name Of Document" & vbTab & GetString(JI2.pDocument)
          Debug.Print "Name Of User To Notify" & vbTab & _
            GetString(JI2.pNotifyName)
          Debug.Print "Type Of Data" & vbTab & GetString(JI2.pDatatype)
          Debug.Print "Print Processor" & vbTab & _
            GetString(JI2.pPrintProcessor)
          Debug.Print "Print Processor Parameters" & vbTab & _
            GetString(JI2.pParameters)
          Debug.Print "Print Driver Name" & vbTab & _
            GetString(JI2.pDriverName)
          Debug.Print "Print Job 'P' Status" & vbTab & _
            GetString(JI2.pStatus)
          Debug.Print "Print Job Status" & vbTab & JI2.Status
          Debug.Print "Print Job Priority" & vbTab & JI2.Priority
          Debug.Print "Position in Queue" & vbTab & JI2.Position
          Debug.Print "Earliest Time Job Can Be Printed" & vbTab & _
            JI2.StartTime
          Debug.Print "Latest Time Job Will Be Printed" & vbTab & _
            JI2.UntilTime
          Debug.Print "Total Pages For Entire Job" & vbTab & JI2.TotalPages
          Debug.Print "Size of Job In Bytes" & vbTab & JI2.Size
          'Because of a bug in Windows NT 3.51, the time member is not set correctly.
          'Therefore, do not use the time member on Windows NT 3.51.
          Debug.Print "Elapsed Print Time" & vbTab & JI2.time
          Debug.Print "Pages Printed So Far" & vbTab & JI2.PagesPrinted
             
          'Display basic job status info.
          'JobStr = JobStr & "Job ID = " & JI2.JobId & _
             'vbCrLf & "Total Pages = " & JI2.TotalPages & vbCrLf
          
          tempStr = ""   'Clear
          'Check for a ready state.
          If JI2.pStatus = 0& Then   ' If pStatus is Null, check Status.
            If JI2.Status = 0 Then
               tempStr = tempStr & "Ready"
            Else  'Check for the various print job states.
               If (JI2.Status And JOB_STATUS_SPOOLING) Then
                  tempStr = tempStr & "Spooling  "
               End If
               
               If (JI2.Status And JOB_STATUS_OFFLINE) Then
                  tempStr = tempStr & "Off line  "
               End If
               
               If (JI2.Status And JOB_STATUS_PAUSED) Then
                  tempStr = tempStr & "Paused  "
               End If
               
               If (JI2.Status And JOB_STATUS_ERROR) Then
                  tempStr = tempStr & "Error  "
               End If
               
               If (JI2.Status And JOB_STATUS_PAPEROUT) Then
                  tempStr = tempStr & "Paper Out  "
               End If
               
               If (JI2.Status And JOB_STATUS_PRINTING) Then
                  tempStr = tempStr & "Printing  "
               End If
               
               If (JI2.Status And JOB_STATUS_USER_INTERVENTION) Then
                  tempStr = tempStr & "User Intervention Needed  "
               End If
               
               If Len(tempStr) = 0 Then
                  tempStr = "Unknown Status of " & JI2.Status
               End If
            End If
        Else
            ' Dereference pStatus.
            tempStr = PtrCtoVbString(JI2.pStatus)
        End If
          
          'Report the Job status.
          'JobStr = JobStr & tempStr & vbCrLf
          JobStr = tempStr
          Debug.Print JobStr & tempStr
      Next i
   End If
   
   'Close the printer handle.
   ClosePrinter hPrinter
End Function


Public Sub pmonitor()
   Dim PrinterStatus As String
   Dim JobStatus As String
   Dim ErrorInfo As String
   Dim bala As String
   Dim r As Integer
Do
   bala = CheckPrinter(PrinterStatus, JobStatus)
   
   Label14.Caption = PrinterStatus
   Label14.Refresh
   Label16.Caption = JobStatus
   Label16.Refresh
   If (PrinterStatus <> "Ready") Then
   r = MsgBox(PrinterStatus, vbOKOnly, "Printer Error")
   End If
   
   If (JobStatus <> "Ready") Then
   Label16.Caption = "Sleeping:" & JobStatus
   Label16.Refresh
   'Sleep 10
   Label16.Caption = "Got Up:" & JobStatus
   Label16.Refresh
   End If

Loop Until (PrinterStatus = "Ready" And JobStatus = "Ready")

Label14.Caption = "Now Out of Looop"
Label14.Refresh
Label14.Caption = ""
Label14.Refresh
Label16.Caption = ""
   Label16.Refresh
End Sub

GreenseedMCSE+I, CCNA, A+Canada, QuebecMake Love Around You.

Link to comment
Share on other sites

wow, that's pretty long

let me take sometime to digest

I'll try to work it out the AutoIt way after the digesting

Thanks!!

The speed of sound is defined by the distance from door to computer divided by the time interval needed to close the media player and pull up your pants when your mom shouts "OH MY GOD WHAT ARE YOU DOING!!!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...