Printing Documents in WSH
(Page 1 of 7 )
Printing documents is a fairly common automation task. Perhaps you want to batch print a folder full of documents, or maybe you’d like to automate report printing. In any case, WSH doesn’t necessarily provide a straightforward method for doing this. Let’s look at some different ways of printing documents in WSH.
The majority of methods that we’ll be using to print will rely on the system’s default printer setting. In order to change printers, you’ll need to be able to detect and set this system setting. There are a number of ways to do this as you will see shortly.
First, let’s take a look at how WMI can be used to view information about installed printers.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!" & strComputer _
& "rootcimv2")
We’ll begin by connecting to the local WMI service at the rootcimv2 namespace. This type of connection should look pretty familiar to you if you’ve used WMI at all in the past.
Set colInstalledPrinters = objWMIService.ExecQuery( _
"Select * from Win32_Printer")
Next, we execute a query that polls all instances of the Win32_Printer class. This class returns a collection object containing a reference to each installed printer.
For Each objPrinter In colInstalledPrinters
WScript.Echo objPrinter.Name
Next
Now we’re able to use a simple For…Next loop to iterate through the collection of installed printers and print the name. “Why is this important?” You ask. It gives you a chance to see the naming conventions that you’ll be using throughout the rest of this article.
Next: Determining the System Default Printer >>
More Windows Scripting Articles
More By Nilpo