?
Solved

Check status of printer using vb.net

Posted on 2008-06-24
6
Medium Priority
?
8,277 Views
Last Modified: 2013-11-07
I need to be able to determine if a printer exists and if it is online.  The name of the printer to be checked is stored as a setting of the program i am creating.   Is it possible to check that status of a printer using vb.net?   The printer may be a local printer or network printer.
0
Comment
Question by:Mike_Stevens
[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
6 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 1000 total points
ID: 21855124
Possible solution found at http://bytes.com/forum/thread351305.html
Private Enum PrinterStatus
PrinterIdle = 3
PrinterPrinting = 4
PrinterWarmingUp = 5
' For more states see WMI docs.
End Enum
 
Private Function PrinterStatusToString(ByVal ps As PrinterStatus) As
String
Dim s As String
Select Case ps
Case PrinterStatus.PrinterIdle
s = "waiting (idle)"
Case PrinterStatus.PrinterPrinting
s = "printing"
Case PrinterStatus.PrinterWarmingUp
s = "warming up"
Case Else ' Vielleicht gibt es noch weitere Fälle...
s = "unknown state"
End Select
PrinterStatusToString = s
End Function
 
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load
Dim strPrintServer As String
strPrintServer = "localhost"
Dim WMIObject As String, PrinterSet As Object, Printer As Object
WMIObject = "winmgmts://" & strPrintServer
PrinterSet = GetObject(WMIObject).InstancesOf("win32_Printer")
For Each Printer In PrinterSet
MsgBox( _
Printer.Name & ": " & _
PrinterStatusToString(Printer.PrinterStatus) _
)
Next Printer
End Sub

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 21855148
Another article about the subject
How to Check if your Printer is Connected
http://www.codeproject.com/KB/printing/printeroffline.aspx
Imports System 
Imports System.Management 
    Class PrinterOffline 
        <STAThread()> _ 
        Private Shared Sub Main(ByVal args As String()) 
            ' Set management scope 
            Dim scope As New ManagementScope("\root\cimv2") 
            scope.Connect() 
            
            ' Select Printers from WMI Object Collections 
            Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_Printer") 
            
            Dim printerName As String = "" 
            For Each printer As ManagementObject In searcher.[Get]() 
                printerName = printer("Name").ToString().ToLower() 
                If printerName.Equals("hp deskjet 930c") Then 
                    Console.WriteLine("Printer = " + printer("Name")) 
                    If printer("WorkOffline").ToString().ToLower().Equals("true") Then 
                        ' printer is offline by user 
                        Console.WriteLine("Your Plug-N-Play printer is not connected.") 
                    Else 
                        ' printer is not offline 
                        Console.WriteLine("Your Plug-N-Play printer is connected.") 
                    End If 
                End If 
            Next 
        End Sub 
    End Class 

Open in new window

0
 
LVL 27

Assisted Solution

by:planocz
planocz earned 1000 total points
ID: 21855197
Here is another sample of how you can do it...


  Public Function InitializeDefaultPrinter() As String
        ' Set the specified printer to the default printer for this program. Return
        ' true if the printer was found.
 
        Dim objprinter As PageSettings = New PageSettings
        Dim strname As String = objprinter.PrinterSettings.PrinterName
 
        Return strname
    End Function
    Public Sub Printer_Check()
        Dim regKey As RegistryKey
        Dim sPrinterDriver As String
        Dim sPrinterPort As String
        Dim myProcess As New Process
        Dim sDefaultPrinter As String
        Dim keyValue As String
 
        'To get the Printer driver...
        sDefaultPrinter = InitializeDefaultPrinter()
        'If sDefaultPrinter.StartsWith("\") Then
        '    sDefaultPrinter = sDefaultPrinter.Substring(2)
        'End If
        keyValue = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Printers\\" & sDefaultPrinter
        regKey = Registry.LocalMachine.OpenSubKey(keyValue, False)
        sPrinterDriver = regKey.GetValue("Printer Driver", 0)
        sPrinterPort = regKey.GetValue("Port", 0)
        regKey.Close()
    End Sub

Open in new window

0
 

Expert Comment

by:ghendric
ID: 23838921
Is there a way to see if there is a timeout error? For example, the printer runs out of paper and times out or throws an error..
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

752 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