Reply to comment

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

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <java>, <powershell>, <tsql>, <visualfoxpro>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.