Set a Printer from the Printers collection as the Default

Category:
System/API
Type:
Classes
Difficulty:
Intermediate
Author:
Vincent Lozada

Version Compatibility: Visual Basic 5

More information:
Attempting to set the default printer to an object variable has no effect. For instance, given a system with more than one printer installed, the following code will not change the default printer: Set Printer = Printers(2). The expected behavior is that the document should print to the first non-default printer found in the printers collection. The actual behavior is that the document prints to the original default printer. Thus, a fix was proposed in MSDN Article ID Q167735. I modified this code from it original and wrapped in a class for the purpose of storing the original printer configuration during class initialization and reseting it back during termination if it was modified.

NOTE FROM FREEVBCODE.COM: According to Microsoft, the problem described has been corrected in Visual Studio '97 Service Pack 2 and presumably Visual Studio 6.0 as well. However, this is still good code to have regardless of what version of VB you are running because it shows you how to use Printer API functions and it automatically resets the default printer to what it was when the program started running.


Instructions: Click the link below to download the code. Select 'Save' from the IE popup dialog. Once downloaded, open the .zip file from your local drive using WinZip or a comparable program to view the contents.

Download setdefaultprinter.zip