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!

Adding a Network Printer to a machne

Status
Not open for further replies.

flushie86

IS-IT--Management
Aug 11, 2004
17
US
I wrote a script that adds 10 printers to a user's computer. The script works great except it only adds the first 6 printers. If I run it again it will add the 7 printer, run it again it'll add the 8th printer and so on. Is there a minimum # of printers the script can add? Is there anyway around this?

Script:
Option Explicit

Dim objNetwork, strLocal, strUNCPrinter1, strUNCPrinter2, strUNCPrinter3, strUNCPrinter4, strUNCPrinter5, strUNCPrinter6, strUNCPrinter7, strUNCPrinter8, strUNCPrinter9, strUNCPrinter10
strUNCPrinter1 = "\\printname"
strUNCPrinter2 = "\\printname"
strUNCPrinter3 = "\\printname"
strUNCPrinter4 = "\\printname"
strUNCPrinter5 = "\\printname"
strUNCPrinter6 = "\\printname"
strUNCPrinter7 = "\\printname"
strUNCPrinter8 = "\\printname"
strUNCPrinter9 = "\\printname"
strUNCPrinter10 = "\\printname"

Set objNetwork = CreateObject("WScript.Network")

objNetwork.AddWindowsPrinterConnection strUNCPrinter1
objNetwork.AddWindowsPrinterConnection strUNCPrinter2
objNetwork.AddWindowsPrinterConnection strUNCPrinter3
objNetwork.AddWindowsPrinterConnection strUNCPrinter4
objNetwork.AddWindowsPrinterConnection strUNCPrinter5
objNetwork.AddWindowsPrinterConnection strUNCPrinter6
objNetwork.AddWindowsPrinterConnection strUNCPrinter7
objNetwork.AddWindowsPrinterConnection strUNCPrinter8
objNetwork.AddWindowsPrinterConnection strUNCPrinter9
objNetwork.AddWindowsPrinterConnection strUNCPrinter10

WScript.Quit
 
no mins or max i would say.

try a loop, or a function, that way your code will look neater and will allow for some logging to give you a better idea what is going on? (you may verywell find that the intReturn will be non zero for a printer which already exists?) mapping printers can be time consuming at logon, i posted somethign a while ago, a function to map printers checkign that it wasnt already there etc which may be of interest

'a delimited string is the most basic
strPrinters = "\\printserver\printerA,\\printserver\printerB"
arrPrinters = Split(strPrinters, ",")
For Each aPrinter In arrPrinters
intReturn = 666 'the devils spawn
On Error Resume Next
intReturn = objNetwork.AddWindowsPrinterConnection(aPrinter)
On Error Goto 0
If intReturn = 0 Then
Wscript.Echo "successfully mapped to " & aPrinter
Else
Wscript.Echo "failed to map to " & aPrinter & " errorcode = " & intReturn
End If
Next
 
Although I had high hopes for this script, I once again am disappointed. The script you provided runs but I receive no pop-ups or printers. Seems the loop does not 'loop' Any other suggestions?
 
'soory my code wasnt the full story?
'was just showing an idea?
'hpw about the below, do you get the echo on screen when running it from cscript.exe of a popup when using wscript.exe? i have tested the below and it works for me, i.e. i get the messages and it fails as there really arent the printers listed
Set objNetwork = CreateObject("WScript.Network")
'a delimited string is the most basic
strPrinters = "\\printserver\printerA,\\printserver\printerB"
arrPrinters = Split(strPrinters, ",")
For Each aPrinter In arrPrinters
Wscript.Echo "will try " & aPrinter
intReturn = 666 'the devils spawn
On Error Resume Next
intReturn = objNetwork.AddWindowsPrinterConnection(aPrinter)
On Error Goto 0
If intReturn = 0 Then
Wscript.Echo "successfully mapped to " & aPrinter
Else
Wscript.Echo "failed to map to " & aPrinter & " errorcode = " & intReturn
End If
Next
Set objNetwork = Nothing
 
I figured it out!!!

it was the loop that wasn't working quite right. I simplified the loop to:

Set objNetwork = CreateObject("WScript.Network")
For Each aPrinter In arrPrinters
objNetwork.AddWindowsPrinterConnection(aPrinter)
Next

Also, the WScript.Echo does not work on my system, I need to use MsgBox.

Thank you for pointing me in the right direction!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top