mayu789
mayu789

Reputation: 103

how to get the ip address of a printer in VB6

I want to get the ip address of a printer (connected through USB to PC).

I am currently using VB6.

Upvotes: 0

Views: 944

Answers (1)

joehanna
joehanna

Reputation: 1489

You need to check the Port property of the printer object you retrieve from a WMI query. In the example below, you could remove the WHERE clause from the WMI query to get all installed printers, and then you could test the ports on each printer. Obviously not all printers will have IPs (XPS Document Writer etc...)

Sub Main()

  Debug.Print GetPrinterPort("HP Color LaserJet CP4005 PCL6")

End Sub

Function GetPrinterPort(printerName As String) As String

  Dim oWMI As Object
  Dim oPrinters As Object
  Dim oPrinter As Object
  Dim sPort As String

  sPort = ""

  Set oWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")

  Set oPrinters = oWMI.ExecQuery("Select * from Win32_Printer WHERE name = '" & printerName & "'")

  If Not oPrinters Is Nothing Then
    For Each oPrinter In oPrinters
      sPort = oPrinter.PortName
      Exit For
    Next
  End If

  Set oWMI = Nothing
  Set oPrinters = Nothing
  Set oPrinter = Nothing

  GetPrinterPort = sPort

End Function

Upvotes: 2

Related Questions