Set/Change Default Printer with VBScript

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Set/Change Default Printer with VBScript

Post by Marcus Tettmar » Wed Aug 06, 2003 11:00 pm

This script displays a list of printers and asks you to enter the printer number which it then sets as the default. Note the SetDefaultPrinter method which takes the printer number.

Code: Select all

VBSTART

Sub DefaultPrinter

Dim Text, Title, i, j, tmp, printer
Dim Network, oDevices

' Create a new Network object to access Network properties
Set Network = CreateObject("WScript.Network")

' Read all printers
Set oDevices = Network.EnumPrinterConnections

Text = "Available printers" & vbCRLF
For i = 0 To oDevices.Count-1 Step 2
j = i
Text = Text & (i / 2) & vbTab
Text = Text & oDevices(i) & vbTab & oDevices(i+1) & vbCRLF
Next

' Show all available printers and allow a user selection
tmp = InputBox (Text,"Set default printer",0)

printer = oDevices(tmp * 2) ' select printer name

' Set the default printer
Network.SetDefaultPrinter printer

MsgBox "Set Default printer to " & printer, _
vbOkonly + vbInformation, Title

Set Network = Nothing

End Sub

VBEND

VBRun>DefaultPrinter

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts