How to get warning of paper out

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
How do I get Windows to display an on-screen message that the printer has run out of paper? I am using objPrintDoc1 and
VB.NET:
ev.Graphics.DrawString(mstrPrintData(mnumCurrentItemNumber).ToString, fntPrintFont, Brushes.Black, numLeftMargin + mnumIndentFromLeftMargin(mnumCurrentItemNumber) + numAdjuster, mnumThisYPosition(mnumCurrentItemNumber), New StringFormat())
etc. and pdSelect.
 
It may be possible using a WMI query for Win32_Printer class and checking DetectedErrorState property.
Check with WMI Code Creator first to see what information may be available for the printer.
 
I found some things about Management Objects, and got as far as verifying that the Epson printer has a DetectedErrorState property of PaperOut (numCount = 4). But how do I get the value of this property?
Private Sub PrinterAttributes()
Dim searcher As New Management.ManagementObjectSearcher("SELECT * FROM Win32_Printer")
Dim numCount As Integer = 0

For Each service As Management.ManagementObject In searcher.Get()

Dim printerStatus As String() = {"Other", "Unknown", "Idle", "Printing", "WarmUp", "Stopped", "Printing", "Offline"}

Dim printerState As String() = {"Paused", "Error", "Pending", "Paper Jam", "Paper Out", "Manual Feed", "Paper Problem", "Offline", "IO Active", "Busy", "Printing", "Output Bin Full", "Not Available", "Waiting", "Processing", "Initialization", "Warming Up", "Toner Low", "No Toner", "Page Punt", "User Intervention Required", "Out of Memory", "Door Open", "Server_Unknown", "Power Save"}

For Each pd As System.Management.PropertyData In service.Properties

If InStr(service("Name").ToString.ToUpper, "EPSON") > 0 Then
If pd.Name = "DetectedErrorState" Then
Do Until numCount > 10
MsgBox(service("Name") & ", pd.Name = " & pd.Name & ", printerState(numCount) = " & printerState(numCount) & vbCrLf & "numCount = " & numCount)

numCount = numCount + 1
Loop
End If
End If

Next pd

Next service

End Sub

printerState() is a string. How do I get the value of the PaperOut property? Thank you.
 
In WMI Code Creator you can select the property and you get a code sample for that. A lot of stuff can be selected in that program, and it generates VB.Net code for all of it, which can be executed on the fly to see results - all before you start to fiddle with coding yourself.
 
OK, I ran WMI Code Creator for Printer and DetectedErrorState, and got
VB.NET:
        objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
        colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Printer", , 48)
        For Each objItem In colItems
            Wscript.Echo("-----------------------------------")
            Wscript.Echo("Win32_Printer instance " & objItem.Name)
            Wscript.Echo("-----------------------------------")
            Wscript.Echo("DetectedErrorState: " & objItem.DetectedErrorState)
        Next
with a list of printers in a cmd window, where my Epson = 2 (Paper out or something)
I pasted the code into my project, adding Dim's
VB.NET:
        Dim objWMIService As Object
        Dim colItems As ManagementObjectCollection
        Dim objItem As Object
(Failed at colItems = )
VB.NET:
        Dim objWMIService As Object
        Dim colItems As ManagementObjectCollection
        Dim objItem As Object
(Failed at colItems =)

What should I Dim colItems as, also objItem.

Thank you again.
 
Forgot to say, error message on failure (see above) is
"Unable to cast COM object of type 'System.__ComObject' to class type 'System.Management.ManagementObjectCollection'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface."
 
You have to select language VB.Net, what you posted is VBScript code. The vbs script will execute happily from WMI Code Creator of course if you just want to play around and check states and such.
 
Thank you, I've got it now. I see there's hours of fun to be had with WMI Code Creator! I assume that the PrinterState of working printers is 16384 or 1024, anything else means 'Check your printer!'
 
Back
Top