Windows Scripting
  Home arrow Windows Scripting arrow Page 6 - Printing Documents in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Printing Documents in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-07-07

    Table of Contents:
  • Printing Documents in WSH
  • Determining the System Default Printer
  • Setting the System Default Printer
  • Shell Printing and Printing with Notepad
  • Printing Word and PDF Documents
  • Printing Web Documents
  • Printing Web Documents continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Windows Scripting Articles
    More By Nilpo


       · There's plenty of reason why you might want your script to print documents. Maybe...
     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek