Microsoft KB Archive/321025

= HOW TO: Programmatically Create a Printer and Printer Port by Using PrinterAdmin (Prnadmin.dll) with a Visual Basic Script in Microsoft Windows 2000 =

Article ID: 321025

Article Last Modified on 3/1/2007

-

APPLIES TO


 * Microsoft Windows 2000 Server
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Windows 2000 Professional Edition

-



This article was previously published under Q321025



IN THIS TASK
SUMMARY
 * Overview of Prnadmin.dll
 * How to Add a Printer and Printer Port by Using Prnadmin.dll with a Visual Basic Script

REFERENCES



SUMMARY
This step-by-step article describes how to programmatically create a printer and printer port by using the PrinterAdmin tool (Prnadmin.dll) and a Visual Basic script in Microsoft Windows 2000.

back to the top

Overview of Prnadmin.dll
PrinterAdmin is available in the Microsoft Windows 2000 Resource Kit. It is a Component Object Model (COM)-based tool that administrators can use to manage printers, drivers, and ports on local and remote Windows 2000-based computers. PrinterAdmin provides large-scale, non-interactive control of printers through the use of scripts. You can either create your own scripts or modify one of the sample scripts that is provided with PrinterAdmin to perform the following tasks:
 * Add and delete a printer.
 * Add and delete a printer driver.
 * Add and delete a printer port.
 * Add and delete a form.
 * View a list of printers, ports, drivers, and forms on a specific computer.
 * Control and configure printers.
 * Print a test page.
 * Clone a print server.

The example Visual Basic script in this article demonstrates how to use Prnadmin.dll to remotely add a printer and printer port on Windows 2000-based computers. The script performs the following tasks:
 * Creates a new port on the client computer. In this example, the port points to the C:\MyFolder\MyPort folder on the computer's hard disk.
 * Installs a printer that uses the new port on the client computer.
 * Creates a C:\MyFolder folder on the client computer's hard disk (in which to store documents that are sent to the new printer).

back to the top

How to Add a Printer and Printer Port by Using Prnadmin.dll with a Visual Basic Script
 Start Notepad, and then open a new text file.  Copy and paste the following lines into the text file:

NOTE: To use Prnadmin.dll, it must be registered on the client computer. The script registers Prnadmin.dll from its location on the server. Make sure that you change \\Servername\Foldername to the correct path to the Prnadmin.dll file on your server. '******** Register prnadmin.dll file on client computer *******

Set WshShell = Wscript.CreateObject(&quot;Wscript.Shell&quot;)

WshShell.Run &quot;regsvr32 /s \\ServerName\FolderName\Prnadmin.dll&quot;,1,TRUE

'************** Create the port first *******************************

dim oPort

dim oMaster

set oPort = CreateObject(&quot;Port.Port.1&quot;)

set oMaster = CreateObject(&quot;PrintMaster.PrintMaster.1&quot;)

oPort.PortName = &quot;C:\MyFolder\MyPort&quot;

oPort.PortType = 3

oMaster.PortAdd oPort

if Err <> 0 then

msgbox &quot;There was an error creating the port.&quot;

end if

'********************************************************************

'************** Create the printer second *************************** 'Change MyPrinter to the name of the printer that you are adding. 'Change PrinterDriver to the name of the printer driver that you are adding.

dim oPrinter

set oPrinter = CreateObject(&quot;Printer.Printer.1&quot;)

oPrinter.PrinterName = &quot;MyPrinter&quot;     ' name of the printer as it appears in the Printers folder

oPrinter.DriverName = &quot;PrinterDriver&quot;      ' name that is referenced in ntprint.inf

oPrinter.PortName = &quot;C:\MyFolder\MyPort&quot;       ' Specify a port name. Can also point to LPT or COM port.

oMaster.PrinterAdd oPrinter If Err <> 0 then

msgbox &quot;There was an error creating the printer.&quot;

end if

'********************************************************************

'************** Create the directory third ***************************

Dim filesys

Set filesys = wscript.createobject(&quot;Scripting.FilesystemObject&quot;)

filesys.CreateFolder &quot;C:\MyFolder\&quot;        ' Create a folder as a repository for the faxes.

If Err <> 0 then

msgbox &quot;There was an error creating the MyFolder folder.&quot;

end if

'********************************************************************

msgbox &quot;The script is finished.&quot;

 Customize the script (as necessary) to meet your requirements. On the File menu, click Save As. Type a name for the script in the File name box by using the .vbs file name extension (for example, MyPrinter.vbs ), specify a location where you want to save the file, and then click Save.

NOTE: Make sure that you include the quotation marks (&quot;&quot;) when you type the file name. This action creates the file with the .vbs file name extension. Quit Notepad. Deploy the script to users' computers.

When the script is run, a new port is created, the printer is installed, and the printer appears in the Printers folder of the client computer.</ol>

back to the top

<div class="references_section">