Flylib.com

Books Software

 
 
 

Connecting to a Network Printer

4.5 Connecting to a Network Printer

Problem

You want to connect to a networked printer.

Solution

You can use the WScript.Network object's AddWindowsPrinterConnection method to connect to a networked Windows printer:

Dim objNetwork Set objNetwork = CreateObject("WScript.Network") objNetwork.AddWindowsPrinterConnection "\\odin\Brother HL-730"

Discussion

The AddWindowsPrinterConnection method adds a networked printer to the local machine and was introduced in WSH 2.0. The syntax for the method is as follows :

objNetwork.AddWindowsPrinterConnectionstrPrinterPath, [strDriverName] [,strPort]

Table 4-4 lists the parameters for the AddWindowsPrinterConnection method.

Table 4-4: AddWindowsPrinterConnection Parameters

PARAMETER

DESCRIPTION

strPrinterPath

Path to the printer in UNC format (e.g., \\server\printername ).

strDriverName

Name of the driver to use. Required on Windows 95/98/ME; ignored if used on Windows NT/2000 or XP platform.

strPort

Optional. Specifies the port to attach the printer to on Windows 95/98/ME. Default is LPT1. Ignored if used on Windows NT/2000 or XP platform.

Using this method is the same as using the Control Panel Printers applet to add a printer connection. In Windows 95/98/ME, the printer driver must already be installed on the machine in order for the AddWindowsPrinterConnection method to work, which reduces the usefulness of this method on these platforms. Windows NT/2000/XP will download the driver if the driver is not installed on the local computer the printer is being installed on. An error will occur if the driver does not successfully install.

Once you have added a printer, you may want to make it, or an existing printer, the default printer for your Windows applications. A default printer is automatically selected when you attempt to print from a Windows application. The syntax for the method is as follows:

objNetwork.SetDefaultPrinter(strPrinterPath)

The strPrinterPath parameter specifies the UNC path of the printer you want to make the default printer. The printer must be installed as a Windows printer appearing under the Control Panel Printer applet. The following code snippet sets the default printer to the network printer \\odin\brother :

Dim objNetwork Set objNetwork = CreateObject("WScript.Network") objNetwork.SetDefaultPrinter "\\odin\brother"

WSH has supported the addition of printers since the initial release using the AddPrinterConnection method. This method is useful only if you need to connect to printers from the command-line environment; the method is the equivalent of using the net use command. Printers that are connected using this method are not available from Windows applications.

The syntax for this method is as follows:

objNetwork. AddPrinterConnection strPort, strPrinterPath,[bUpdateProfile], [strUser], [strPassword]

Table 4-5 lists the parameters for the AddPrinterConnection method.

Table 4-5: AddPrinterConnection Parameters

PARAMETER

DESCRIPTION

strPrinterPort

The local printer port to connect the network printer to (e.g., LTP1:).

strPrinterPath

Path to the printer in UNC format (e.g., \\server\printername ).

bUpdateProfile

Optional Boolean parameter that determines if printer connection is stored in user profile and reconnected in future sessions.

strUserID

Optional user ID for connecting to remote printer with.

strPassword

Optional password for connecting to remote printer. This is used in combination with the strUserID parameter.

{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

The following code snippet connects the LPT2 port to the remote printer laserjet on computer Odin:

Set objNetwork = CreateObject("Wscript.Network") objNetwork.AddPrinterConnection "LPT2", "\\odin\laserjet"