WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Printing Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to Add a Printer and Make it the Default Printer

Keywords:	default printer

Question:

I need to automate adding a new network printer and make it the default printer. Can this be done? How?

Answer:

See the Printer Control Extender function pAddPrinter. pAddPrinter adds a printer to the list of supported printers for a server.
Syntax:

pAddPrinter(server-name, printer-name, port-name, driver-name, print-processor)

Parameters:

(s) server-name	In Windows 95, this is not used, and should be set to "".
You  can only add printers for the local machine. In
Windows NT, this specifies the machine on which to
create the printer.  The caller must have admistrative
access to the server. You can specify "" for the local
machine.
(s) printer-name	the name you want for the newly-added printer.
(s) port-name	printer port. (ie., LPT1)
(s) driver-name	must be the name of an already-installed driver.  This
should be the driver's display name (eg, as shown in
the pull-down listbox for "Print using the following driver"
under a printer's properties), not the file name.

(s) print-processor	will almost always be "WinPrint".


Returns:

(i)	1 on success, or a negative error code on failure.

Note that this function may take a minute or so to complete.


Example:

AddExtender("WWPRT44I.DLL")
pAddPrinter("", "My new printer", "LPT1:", "HP LaserJet 4", "WinPrint")


Question:

I would like to be able to add a printer using a login/startup script in windows.

Answer:

There are several ways to do so.

Run commandline:

 
ShellExecute('rundll32', 'printui.dll,PrintUIEntry /in /n \\server\printername', '', @NORMAL, '')  ;adds the printer
ShellExecute('rundll32', 'rundll32 printui.dll,PrintUIEntry /in /n \\server\printername /y', '', @NORMAL, '') ; makes it default
This is to add the printer for a particular user. It may be that the user is not authorized to add the printer and you may get an error: You do not have sufficient access to your computer to connect to the selected printer.

In that case, you'll have to run the script as an aministrator on the computer and add the printer to the system globally, not just the one user:

ShellExecute('rundll32', 'printui.dll,PrintUIEntry /ga /c\\computername /n\\server\printername', '', @NORMAL, '')  ; add the printer to the system globally
If you are adding the above to a login script replace computername with the environments COMPUTERNAME. This way it will automatically insert the name of the current computer.
computername = Environment( "COMPUTERNAME" )
ShellExecute('rundll32', 'printui.dll,PrintUIEntry /ga /c\\':computername:' /n\\server\printername', '', @NORMAL, '')  ; add the printer to the system globally
To delete the printer globally, run the following:
computername = Environment( "COMPUTERNAME" )
ShellExecute('rundll32', 'printui.dll,PrintUIEntry /gd /c\\':computername:' /n\\server\printername', '', @NORMAL, '')  ; Delete the printer from the system globally
Using COM:
;Add printers
strUNCPrinter = "\\servername\printername"

strPort = "LPT2"
strPort = ""
strUNCPrinter = ""

objNetwork = ObjectCreate("WScript.Network")

objNetwork.AddWindowsPrinterConnection( strUNCPrinter )
objNetwork.AddPrinterConnection( strPort,strUNCPrinter )
objPrinter.SetDefaultPrinter( strUNCPrinter )

objNetwork = 0

Article ID:   W13681
Filename:   Add a Printer to NT-95 and Make it the Default Printer.txt
File Created: 2012:10:05:09:17:46
Last Updated: 2012:10:05:09:17:46