Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for chdefprn.vbs

(view source code of chdefprn.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim strKey, wshShell
  4.  
  5. ' Create WScript Shell object to read the registry
  6. Set wshShell = CreateObject( "WScript.Shell" )
  7.  
  8. ' Read the current default printer from registry
  9. strKey = "HKEY_CURRENT_USER\Software\Microsoft" _
  10.        & "\Windows NT\CurrentVersion\Windows\Device"
  11. WScript.Echo "Current Default printer : " _
  12.            & Trim( Split( wshShell.RegRead( strKey ), "," )(0) )
  13.  
  14. ' Call the Print Dialog to change the default printer
  15. WScript.Echo "Choose a new default printer..."
  16. ChangePrinterSettings
  17.  
  18. ' Read the new default printer from registry
  19. WScript.Echo "New Default printer     : " _
  20.            & Trim( Split( wshShell.RegRead( strKey ), "," )(0) )
  21.  
  22. ' Release the Shell object
  23. Set wshShell = Nothing
  24.  
  25.  
  26. Sub ChangePrinterSettings( )
  27. ' Interactively change your printer settings, including the default
  28. ' printer. Click the "Print" button to confirm the new printer settings.
  29. '
  30. ' Written by Rob van der Woude
  31. ' http://www.robvanderwoude.com
  32.  
  33. 	Dim objPrnDlg, strPrompt, strTitle
  34.  
  35. 	Const vbOK     = 1
  36. 	Const vbCancel = 2
  37. 	Const vbAbort  = 3
  38. 	Const vbRetry  = 4
  39. 	Const vbIgnore = 5
  40. 	Const vbYes    = 6
  41. 	Const vbNo     = 7
  42.  
  43. 	' Explain there will be no OK button, the Print button must be
  44. 	' clicked instead.
  45. 	strPrompt = "In the next dialog, choose which printer will " _
  46. 	          & "be the new Default Printer and press the " _
  47. 	          & """Print"" button to confirm." & vbCrLf & vbCrLf _
  48. 	          & "Note that any changes you make in the printer " _
  49. 	          & "settings will be permanent, i.e. they will be " _
  50. 	          & "the new default settings."
  51. 	strTitle  = "Choose New Default Printer and/or Printer Settings"
  52. 	If MsgBox( strPrompt, vbOKCancel, strTitle ) = vbOK Then
  53. 		' Create a dialog object
  54. 		Set objPrnDlg = CreateObject( "MSComDlg.CommonDialog.1" )
  55. 		' Make selections permanent
  56. 		objPrnDlg.PrinterDefault = True
  57. 		' Open the Print dialog
  58. 		objPrnDlg.ShowPrinter
  59. 		' Release the object
  60. 		Set objPrnDlg = Nothing
  61. 	End If
  62. End Sub
  63.  

page last modified: 2024-04-16; loaded in 0.0099 seconds