Windows Scripting
  Home arrow Windows Scripting arrow Page 5 - 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 Word and PDF Documents


    (Page 5 of 7 )

    There may still be a few instances where the Shell method and Notepad may not be enough for your printing needs.  In these cases, you can look to outside applications.  This may be especially true if you are trying to print a document in a proprietary format.

    I’m going to show you very quickly how to automate two common applications for printing Microsoft Word and Adobe PDF documents.  Again, if your files use registered file extensions, even these should be printable using the Shell method.

    Microsoft Word supports a very wide range of document formats and can be a very powerful tool for printing.  The Word OLE automation object also provides very thorough access to Word’s print capabilities, so printing with this method is both powerful and highly configurable.

    Const wdDoNotSaveChanges = 0

     

    Set objWord = CreateObject("Word.Application")

    objWord.Visible = False

    objWord.DisplayAlerts = False

    Begin using Word by connecting to its OLE automation object.  I’m also setting its Visible property to False so that the application window is not visible, and its DisplayAlerts property to false to prevent displaying message boxes that require user input.

    objWord.Documents.Open("mydoc.doc")

    objWord.Options.PrintBackground = False

    objWord.ActiveDocument.PrintOut

    objWord.Application.Quit wdDoNotSaveChanges

    From here, printing is pretty straightforward.  Use the Open method to open a document and then use the Document object’s PrintOut method to send it to the default printer.

    Setting the PrintBackground property to False disables Word’s background printing feature and will prevent errors if the Word object is released before the print job completes.

    It should be noted that the Word object’s print capabilities are much more powerful than you see here.  For a more in-depth look at printing with Microsoft Word, read my upcoming article “Advanced Word Object Scripting” here on ASP Free.

    Now we’ll take a look at scripting Adobe Reader for printing PDF and other supported documents.

    Set objAcroPDF = CreateObject("AcroPDF.PDF")

     

    objAcroPDF.LoadFile(mypdf.pdf)

    objAcroPDF.PrintAll

    Set objAcroPDF = Nothing

    Here we’re connecting to the AcroPDF object and using its LoadFile method to open a document.  Then a call to its PrintAll method sends the document to the default printer.

    I don’t think the AcroPDF object was intended for use in scripting outside of Adobe’s own macros.  However, connecting to its COM object does expose methods to open and print documents.  I have not found an intrinsic method for closing the application, so be sure to set its object reference to Nothing after printing in order to release the object.

    The AcroPDF object also exposes several other print methods that allow you to control print ranges and whether or not the document should be shrunk to fit pages.  It also provides a printWithDialog method that will display the Print dialog box and allow the user to select print settings.

    objAcroPDF.printAllFit(blnShrinkToFit)

    objAcroPDF.printPages(lngFrom, lngTo)

    objAcroPDF.printPagesFit(lngFrom, lngTo, blnShrinkToFit)

    objAcroPDF.printWithDialog

    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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek