Printing Documents in WSH - Determining the System Default Printer
(Page 2 of 7 )
The system default printer setting is stored in the local hardware profile in the Windows registry. This is a global setting that is used by any application that relies on system APIs for printing. Since this setting is stored in the registry, it becomes the first logical place to look for the current setting.
Set WshShell = CreateObject("WScript.Shell")
strRegVal = "HKCUSoftwareMicrosoftWindows NTCurrentVersionWindowsDevice"
strDefault = WshShell.RegRead(strRegVal)
strDefault = Left(strDefault, InStr(strDefault, ",") - 1)
You can use any method you like to read the required registry key. For simplicity, I’ve used the WshShell object to read the registry, but I could have just as easily used WMI. The key should contain something like the following:
Lexmark 3500-4500 Series,winspool,Ne04:
As you can see, the printer name is the first of a series of comma-delimited values. Making use of VBScript’s InStr and Left functions allows us to parse out only the required information.
Once you have the printer name, you can construct a UNC path to the printer in the form of computernameprintername. The WshNetwork object can be used to retrieve the local computer name.
The next method for retrieving the system default printer setting relies on WMI.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer _
& "rootcimv2")
Set colPrinters = objWMIService.ExecQuery(_
"Select * From Win32_Printer Where Default = '-1'")
After connecting to the local WMI service’s rootcimv2 namespace, we query the Win32_Printer class for all instances where the Default property is set to -1. Keep in mind that -1 is the integer representation of a Boolean true value. This query returns a collection of printer objects that have the Default value set to True.
For Each objPrinter In colPrinters
strDevice = objPrinter.DeviceID
strSystem = objPrinter.SystemName
Next
Next we’ll use a For…Next loop to iterate through the collection and read the DeviceID and SystemName properties for each printer object. This may seem a bit redundant since there can only be one default printer, meaning that the colPrinters collection should only contain a single reference. This is a valid argument, so if you prefer you can use this code instead.
strDevice = colPrinters.Item(0).DeviceID
strSystem = colPrinters.Item(0).SystemName
My only argument against this more direct method is that it will cause an error if it is run on a system that does not have any installed printers. In that case, the colPrinters collection will not have any members and an error will be raised when trying to access a non-existent item.
Whichever method you choose to use, you will end up with the same result. The DeviceID property will return the name of the printer and the SystemName property will return the name of the system that the device is installed on—the current computer name.
If Not IsEmpty(strDevice) Or Not IsEmpty(strSystem) Then
strUNC = "" & strSystem & "" & strDevice
Else
strUNC = ""
End If
Building a valid UNC path is as simple as concatenating our two variables with the proper forward slashes. I’ve chosen to do this inside of an If block to again avoid errors in the scenario mentioned previously.
Next: Setting the System Default Printer >>
More Windows Scripting Articles
More By Nilpo