Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking existance of printer in a collection/array

Status
Not open for further replies.

sharapov

MIS
May 28, 2002
106
US
Hello,

I am trying to figure out whether one of the printers from the print server had been mapped already using code below, but I am not having too much luck. Can you help?

NOTE: I don’t want to use “For Each objPrinter in colPrinters” statement because I don’t want to iterate through every record in the collection. I just want to find out if a particular printer exists in colPrinters. If it does I don’t want to do anything, if it doesn’t I want to map it.



On Error Resume Next
Set objNetwork = CreateObject("Wscript.Network")
Dim colInstalledPrinters
Dim objPrinter
Dim strMapPrinter1
Dim strMapPrinter2
Dim strMapPrinter3
Dim strPrintServer
strPrintServer = "\\caprint01\"
strMapPrinter1 = "TOSHIBA e-STUDIO 550 PCL6"
strMapPrinter2 = "TOSHIBA e-STUDIO 520 PCL6"
strMapPrinter3 = "Dell Laser Printer 3100cn PCL6"

strComputerName = "."

Set objWMIService = GetObject( _
"winmgmts:" & "{impersonationLevel=impersonate}!\\" _
& strComputerName & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
Dim i
For i=0 To UBound(colInstalledPrinters) Step 1
If colInstalledPrinters(i) = strMapPrinter1 Then
Wscript.Echo "Printer Already Mapped"

Else
objNetwork.AddWindowsPrinterConnection strPrintServer & strMapPrinter1
'objNetwork.SetDefaultPrinter strPrintServer & strMapPrinter1
End If
Next
InArray=False

Set objNetwork = Nothing
Set objWMIService = Nothing
Set colInstalledPrinters = Nothing
WScript.Quit



Thank you.

Igor
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top