Example 2 – Printer Script with AddWindowsPrinterConnection
This script has the same core as the first example but with a few extra coding niceties. As you get to know my VBScript style, so you will see my familiar heading section, liberal use of variables, and a WScript.Echo message box to confirm what has happened.
‘ PrintersLong.vbs – Windows Logon Script.
‘ VBScript – Connect a network printer with AddWindowsPrinterConnection
‘ Author Guy Thomas http://computerperformance.co.uk/
‘ Version 2.2 – April 24th 2005
‘ ——————————————————————————’
Option Explicit
Dim objNetwork, strUNCPrinter
strUNCPrinter = “\\zara\HP LaserJet”
Set objNetwork = CreateObject(“WScript.Network”)
objNetwork.AddWindowsPrinterConnection strUNCPrinter
WScript.Echo “Check the Printers folder for : ” & strUNCPrinter
WScript.Quit
‘ End of Guy’s Windows printer script.
VBS Learning Points
Note 1: Option Explicit forces us to declare variables before we use them in the VBScript. The idea is to reduce spelling mistakes.
Note 2: One reason that I like to employ variables is to make it easier to troubleshoot in general and display messages with WScript.Echo in particular.
Note 3: To help with the line numbers, and to color code your commands get OnScript (free download).
ford detroit
Por: injumn el 25 Junio 2007
a las 2:48 am