Note that a printer object does not currently exist in VBA for Microsoft Excel.
|(Declarations)||Declarations||Declarations and private variables for the modPrinterWindows module.|
|ChangeDefaultPrinter||Procedure||Change the Windows default printer to the specified printer.|
|DefaultPrinterName||Procedure||Get the name of the current Windows default printer.|
|PrinterInfoCommon||Procedure||Get the shared printer properties from the printer object from Microsoft Access and VB6. While Microsoft Access and VB6 have printer objects, their properties are not the same.|
|PrinterInfoAccess||Procedure||Get a string of information regarding the specified printer within Microsoft Access. The printer object's properties in MS Access are different from the printer object in VB6.|
|PrinterInfoVB6||Procedure||Get a string of information regarding the specified printer within Visual Basic 6. The printer object's properties in MS Access are different from the printer object in VB6.|
|PrintersToArray||Procedure||Show all printers connected to the system.|
|ShowPrinters||Procedure||Show all printers available for the current machine.|
' Example of modPrinterWindows ' ' To use this example, create a new module and paste this code into it. ' Then run the procedure by putting the cursor in the procedure and pressing: ' F5 to run it, or ' F8 to step through it line-by-line (see the Debug menu for more options) Private Sub Example_modPrinterWindows() ' Comments: Example of using the modPrinterWindows module to get mouse information, with support for 32 and 64 bit API calls using VBA and VB6. ' Run this procedure and see the information of each printer in the Immediate Window. Dim strDefaultPrinter As String Dim astrPrinters() As String Dim intPrinterCount As Integer Dim intPrinter As Integer Dim strNewPrinter As String ' Show the list of printers in a message box ShowPrinters ' Get the current default printer name strDefaultPrinter = DefaultPrinterName Debug.Print "Default printer: " & strDefaultPrinter ' Get the list of printers intPrinterCount = PrintersToArray(astrPrinters) Debug.Print "Available printers: " & intPrinterCount ' For each printer, show its details For intPrinter = 0 To intPrinterCount - 1 Debug.Print astrPrinters(intPrinter) ' This retrieves the common properties from the Printer object in Microsoft Access and VB6 Debug.Print PrinterInfoCommon(intPrinter) ' Use this for the complete list of porperties available for Microsoft Access 'Debug.Print PrinterInfoAccess(intPrinter) ' Use this for the complete list of porperties available for VB6 'Debug.Print PrinterInfoVB6(intPrinter) Debug.Print "-----------------------------" Next intPrinter ' Show how to change the default printer (assuming one already exists) If strDefaultPrinter <> "" Then ' Find the first printer that's not the current default printer strNewPrinter = "" For intPrinter = 0 To intPrinterCount - 1 If astrPrinters(intPrinter) <> strDefaultPrinter Then strNewPrinter = astrPrinters(intPrinter) Exit For End If Next intPrinter ' If there's another printer to switch to, show how it's done If strNewPrinter <> "" Then If MsgBox("This example will change your default printer from " & strDefaultPrinter & " to " & strNewPrinter & " then reset it to the original printer." & vbCrLf & _ "If the code is interrupted or fails, your default printer may be changed incorrectly. " & vbCrLf & _ "Would you like to continue?", vbQuestion + vbYesNo) = vbYes Then ' Change the Default Printer Debug.Print "Changing the default printer." If ChangeDefaultPrinter(astrPrinters(0)) Then ' Confirm the default printer is changed by retrieving the value again Debug.Print "Default printer changed to: " & DefaultPrinterName Debug.Print "Changing the default printer back to the original value." If ChangeDefaultPrinter(strDefaultPrinter) Then ' Confirm the default printer is reset by retrieving the value again Debug.Print "Default printer changed to: " & DefaultPrinterName Else Debug.Print "Default printer could not be changed." End If Else Debug.Print "Default printer could not be changed." End If End If End If End If End Sub
The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.
Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!
Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!
"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."
Van T. Dinh, Microsoft MVP