Changing Windows default printer

Sometimes it's necessary to change Windows default printer from within VFP application. It can be done with Windows API, WSH or WMI.

WIN API SetDefaultPrinter function is available in Windows 2000 and later.
WSH SetDefaultPrinter method is supported under Win98 and later but WSH has to be present on PC.
WMI SetDefaultPrinter class method is available in Windows XP and later.

The WinApiErrMsg function in the code below is from Retrieving Windows system error message.

This is sample code. Add error handling and adjust to your requirements as necessary.

* New default printer
lcPrinterName = "Microsoft XPS Document Writer"
* Save current Windows default printer in case we would want to restore it later
lcSaveWinDefPrinter = SET("PRINTER", 2)
 
*------------------------------------------
 
* Windows API
DECLARE Long GetLastError IN WIN32API
DECLARE Long SetDefaultPrinter In WINSPOOL.DRV String pPrinterName
IF SetDefaultPrinter(lcPrinterName) = 0
	* Error
	? WinApiErrMsg(GetLastError())
ELSE
	? "New default printer: " + SET("PRINTER", 2)
	* ...
	* Restore default printer
	SetDefaultPrinter(lcSaveWinDefPrinter)
	? "Restored default printer: " + SET("PRINTER", 2)
ENDIF	
 
*------------------------------------------
 
* WSH
loNet = CREATEOBJECT("WScript.Network")
TRY
	loNet.SetDefaultPrinter(lcPrinterName + "")
	? "New default printer: " + SET("PRINTER", 2)
	*...
	loNet.SetDefaultPrinter(lcSaveWinDefPrinter)
	? "Restored default printer: " + SET("PRINTER", 2)
CATCH TO oExp WHEN oExp.ErrorNo = 1429
	* Error 
	? oExp.ErrorNo, oExp.Message
ENDTRY	
 
*------------------------------------------
 
* WMI
loLocator = CREATEOBJECT('WBEMScripting.SWBEMLocator')
* Local computer, logged on user
loWMI = loLocator.ConnectServer() 
loWMI.Security_.ImpersonationLevel = 3  		&& Impersonate
* Retrieve info about new printer
loPrinters = loWMI.ExecQuery([Select * from Win32_Printer Where Name = '] + STRTRAN(lcPrinterName, [\], [\\]) + ['])
* Check if new printer exists
IF loPrinters.Count > 0
	* FOR EACH loop is only way to access collection item(s) 
	FOR EACH loPrinter IN loPrinters
		loPrinter.SetDefaultPrinter()
	ENDFOR
 
	? "New default printer: " + SET("PRINTER", 2)
	*...
	loPrinters = loWMI.ExecQuery([Select * from Win32_Printer Where Name = '] + STRTRAN(lcSaveWinDefPrinter, [\], [\\]) + ['])
	FOR EACH loPrinter IN loPrinters
		loPrinter.SetDefaultPrinter()
	ENDFOR
	? "Restored default printer: " + SET("PRINTER", 2)
 
ENDIF
Your rating: None Average: 5 (1 vote)

Forcing application objects to refresh default printer setting

After changing the default Windows printer, the new default setting is not respected by new IE browser objects (created in VFP code after the code for changing the default printer)! They still have the old default printer selected.

Is there a way to force the new setting to be respected?

Cannot refresh default printer setting

It's my experience that already running applications ignore changes of default Windows printer. IOW, only applications started after default Windows printer changed will use it.

Re: Forcing application objects to refresh default printer setti

Very good post, thanks a lot.

Changing Windows default printer (Problem)

Very useful but I found that the WMI approach could not restore the default printer if it was a network printer (e.g. \\UKKEWFPS01\PEG-MIS)

Re: Changing Windows default printer (Problem)

Hi Paul,

WMI requires to double any backslashes in the strings passed to it and I forgot to do that. It's fixed now.
Thanks for letting me know.