0
votes

I'm using the following VBScript to set a default printer:

Option Explicit
On Error Resume Next
Dim objNetwork, strUNCPrinter
strUNCPrinter = "\\printer\location\here"
Set objNetwork = CreateObject("WScript.Network") 

objNetwork.SetDefaultPrinter strUNCPrinter

WScript.Quit

What I would like for it to do is cycle through printers, basically "If this printer isn't installed, make that one the default"

I've tried just repeating the script, praying that it would repeat the process, but it just errors.

1
If the printer is not installed, you should first add it and then make it as default printer. I cannot see any line in your code which adds the printer connection before making it the default printer. - Gurmanjot Singh
@gman, yeah, basically what I want to do is set it to the default printer ONLY if it's already installed. if the first printer isn't installed, make the second printer default, ect - Asteria
Okay. The solution which I have provided will first add the printer connection. If it gets added successfully, then it makes it the default printer. To accomplish what you want, you can store all the printers' names in an array and loop through that array to check if the printers are installed or not. If a particular printer in the array is not installed move to the next iteration and check again. Do this until it finds a printer which is installed and then make it the default one. Refer this site for help: ss64.com/vb/network.html - Gurmanjot Singh
Thanks @Gman! I'll give it a crack, sounds like what I want :) - Asteria

1 Answers

1
votes

Can you give this code a try:

Option Explicit
On Error Resume Next
Dim objNetwork, strUNCPrinter
strUNCPrinter = "\\printer\location\here"
Set objNetwork = CreateObject("WScript.Network") 
Set objAdd = objNetwork.AddWindowsPrinterConnection(strUNCPrinter)
If Not objAdd Then
    MsgBox "Printer connection unsuccessful"
Else
    objNetwork.SetDefaultPrinter strUNCPrinter
End IF

Set objAdd = Nothing
Set objNetwork = Nothing