How to list ip addresses of printers on compuer

Hey Everyone,
I am trying to write a quick program that outputs a file listing the printers installed with there ip addresses.

So far this is what i have

Private Sub Form_Load()
Dim l_pr As Printer
Open "c:\PrinterINFO.txt" For Output As #1

For Each l_pr In Printers
   Print #1, l_pr.DeviceName & " - " & l_pr.Port
Next
Close #1
End
End Sub

sadly this is my output
Microsoft XPS Document Writer - Ne00:
Microsoft Office Document Image Writer - Ne01:
HP Color LaserJet 4730mfp PCL 6 - Ne02:
XX786 RICOH Aficio 2090 - Ne03:

LVL 2
JesNoFearEnterprise Systems Administration Team MemberAsked:
Who is Participating?
 
grayeConnect With a Mentor Commented:
Have you considered using Windows Management Instrumentation (WMI)?
The example below is VBScript... but it should be fairly easy to translate to VB6.   In my case, the PortName shows the IP address of the printers
 

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_Printer",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_Printer instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "DeviceID: " & objItem.DeviceID
    Wscript.Echo "Local: " & objItem.Local
    Wscript.Echo "Location: " & objItem.Location
    Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "Network: " & objItem.Network
    Wscript.Echo "PortName: " & objItem.PortName
    Wscript.Echo "ServerName: " & objItem.ServerName
Next

Open in new window

0
 
JesNoFearEnterprise Systems Administration Team MemberAuthor Commented:
Awesome! thanks
0
 
JesNoFearEnterprise Systems Administration Team MemberAuthor Commented:
works great, here is my code
--------------------------------------------------------------------
Open "c:\PrinterINFO.txt" For Output As #1

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_Printer", , 48)
For Each objItem In colItems
    Print #1, objItem.Caption & " - " & objItem.PortName
Next
Close #1
End

-----------------------------------------------------------------------------------------------------
Output:
Microsoft XPS Document Writer - XPSPort:
Microsoft Office Document Image Writer - Microsoft Document Imaging Writer Port:
HP Color LaserJet 4730mfp PCL 6 - IP_10.220.2.197
XX786 RICOH Aficio 2090 - IP_10.80.2.90
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.