Printing Documents in WSH - Printing Web Documents
(Page 6 of 7 )
The final method of printing with WSH that I’d like to show you is how to print from Internet Explorer. There are a few distinct advantages to choosing this method. The first is that it allows you to print any document supported by Internet Explorer, including web pages. If you are connected to the Internet you can even supply a valid URL and print remote web pages.
The second advantage is a little different. Until now, none of the methods I’ve shown you have provided a way to monitor when a print job actually completes; Internet Explorer provides an event that will allow you to do just that.
On the surface, printing in IE is going to seem a bit complicated, but if you take it slow and reread when necessary you should find it fairly easy to understand.
Const OLECMDID_PRINT = 6
Const OLECMDEXECOPT_DONTPROMPTUSER = 2
Const PRINT_WAITFORCOMPLETION = 2
Const OLECMDF_SUPPORTED = 1
Const OLECMDF_ENABLED = 2
blnPrintingComplete = False
Here we begin by declaring a few necessary constants and creating a Boolean value that we will use to determine when the print job has completed. It should initially be set to False since the print job has not been created.
Set objIE = WScript.CreateObject("InternetExplorer.Application", "IE_")
Now we connect to the Internet Explorer object. There are two things that you should take note of here. First, notice that I’m explicitly using the WScript object’s CreateObject method as opposed to VBScript’s that we use when we don’t specify. This brings us to the second noteworthy part. The WScript object’s CreateObject method allows us to specify a second parameter that is to be used as a prefix for the object. This is the prefix we will need later in order to make use of Internet Explorers events.
objIE.Visible = False
objIE.Navigate strFile
Next, I’m setting IE’s visible property to False to make sure that it doesn’t display visibly, and then I’m telling it to navigate to a document. The variable strfile can be either a local file path or any valid URL.
Do While objIE.ReadyState <> 4
WScript.Sleep 10
Loop
This next piece of code uses a Do…Loop to essentially pause script execution until the page has fully loaded.
If objIE.QueryStatusWB(OLECMDID_PRINT) = OLECMDF_SUPPORTED _
+ OLECMDF_ENABLED Then
objIE.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, _
PRINT_WAITFORCOMPLETION, 0
Do While Not blnPrintingComplete
WScript.Sleep 50
Loop
End If
objIE.Quit
This If block basically determines whether or not printing is supported before attempting to issue a print command using the ExecWB method. The additional parameters instruct IE to print the current document, suppress any print dialog boxes, and wait for completion before continuing, respectively.
Next: Printing Web Documents continued >>
More Windows Scripting Articles
More By Nilpo