Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > User Interaction > Change Default Printer Dialog

Change Default Printer Dialog


This dialog can be used to change printer settings too.
All changes made in this dialog are permanent, i.e. they will be the new default settings.


VBScript Code:
Option Explicit

Dim strKey, wshShell

' Create WScript.Shell object to read the registry
Set wshShell = CreateObject( "WScript.Shell" )

' Read the current default printer from registry
strKey = "HKEY_CURRENT_USER\Software\Microsoft" _
       & "\Windows NT\CurrentVersion\Windows\Device"
WScript.Echo "Current Default printer : " _
           & Trim( Split( wshShell.RegRead( strKey ), "," )(0) )

' Call the Print Dialog to change the default printer
WScript.Echo "Choose a new default printer..."

' Read the new default printer from registry
WScript.Echo "New Default printer     : " _
           & Trim( Split( wshShell.RegRead( strKey ), "," )(0) )

' Release the Shell object
Set wshShell = Nothing

Sub ChangePrinterSettings( )
' Interactively change your printer settings, including the default
' printer. Click the "Print" button to confirm the new printer settings.
' Written by Rob van der Woude

    Dim objPrnDlg, strPrompt, strTitle

    Const vbOK     = 1
    Const vbCancel = 2
    Const vbAbort  = 3
    Const vbRetry  = 4
    Const vbIgnore = 5
    Const vbYes    = 6
    Const vbNo     = 7

    ' Explain there will be no OK button, the Print button must be
    ' clicked instead.
    strPrompt = "In the next dialog, choose which printer will " _
              & "be the new Default Printer and press the " _
              & """Print"" button to confirm." & vbCrLf & vbCrLf _
              & "Note that any changes you make in the printer " _
              & "settings will be permanent, i.e. they will be " _
              & "the new default settings."
    strTitle  = "Choose New Default Printer and/or Printer Settings"
    If MsgBox( strPrompt, vbOKCancel, strTitle ) = vbOK Then
        ' Create a dialog object
        Set objPrnDlg = CreateObject( "MSComDlg.CommonDialog.1" )
        ' Make selections permanent
        objPrnDlg.PrinterDefault = True
        ' Open the Print dialog
        ' Release the object
        Set objPrnDlg = Nothing
    End If
End Sub
Windows version: any (except for the registry keys holding the default printer) as long as ComDlg32.ocx is installed and registered
Network: N/A
Client software: Requires ComDlg32.ocx, which is included with Visual Basic 5 and later and MS-Office 2000 and XP.
You can download ComDlg32.ocx version 6 or version 5 from
Script Engine: any
Summarized: The subroutine should work in any Windows version as long as ComDlg32.ocx is installed and registered.
The registry keys for the default printer, used here for demonstration purposes only, are valid for Windows NT 4 and later only.
Change Default printer warning (MSComDlg.CommonDialog.1)
Change Default printer dialog (MSComDlg.CommonDialog.1)
[Back to the top of this page]

page last uploaded: 2016-09-19, 14:58