Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - Reading and Printing Word 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

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

    Table of Contents:
  • Reading and Printing Word Documents in WSH
  • Printing Word Documents
  • Advanced Printing Options
  • Page Setup options
  • Printing Page Ranges and more

  • 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


    Reading and Printing Word Documents in WSH - Printing Word Documents


    (Page 2 of 5 )

    I also get a lot of readers asking how they can print Word documents from within a WSH script.  Many of them are looking for ways to automate report printing or simply to perform batch printing of multiple documents.  Whatever your specific need might be, this is a fairly simple task to perform.

    Const wdDoNotSaveChanges = 0

     

    strDocument = "C:mydoc.docx"

     

    Set objWord = CreateObject("Word.Application")

    objWord.Visible = False

    objWord.DisplayAlerts = False

    objWord.Documents.Open strDocument,, True

    Set objDoc = objWord.ActiveDocument

    Once again, you’ll begin by connecting to the Word automation object and opening the document that you wish to print.  Here again, I’m keeping the Word application hidden since most automation scripts are designed to run unattended.

    objWord.Options.PrintBackground = False

    Next, we’re going to turn off Word’s Background Printing feature.  An error message is sometimes displayed if the Word object is released before the print job completes.  Turning off Background Printing will eliminate this error.

    objDoc.PrintOut

    Finally, we get to the meat and potatoes of the script.  We use the Document object's PrintOut method to send our document to the printer.  Note that this method will print to the printer that is found in Word’s default printer setting.  In most cases, this will be the last printer that was used within Word.  If you would like to print to another printer, or just prevent some unwanted mishaps by specifying a printer, you can use the code below instead.

    strActivePrinter = objWord.ActivePrinter

    objWord.ActivePrinter = "HP LaserJet 4 local on LPT1:"

    objDoc.PrintOut

    objWord.ActivePrinter = strActivePrinter

    This piece of code first polls the ActivePrinter property for its current value and assigns it to a variable.  Then it sets that value manually before calling the PrintOut method.  Finally, the ActivePrinter property is set back to its original value.

    This does present one major caveat in some cases.  Setting the value of the ActivePrinter property in Word will also set the system default printer.  You may not wish to change the system’s default printer setting.  In the next example, I’ll show you how to set a Word printer setting without changing the system default printer.

    objWord.WordBasic.FilePrintSetup "HP LaserJet 4 local on LPT1:", 1

    Here I’m using the FilePrintSetup method. This method allows me to specify an optional parameter that tells Word not to set the system default printer when changing its active printer.

    objWord.Quit wdDoNotSaveChanges

    Now we can close the document and exit the Word application.

    If you’re performing batch print jobs, you may want to think of ways to that this functionality could be packaged in a reusable function.

    More Windows Scripting Articles
    More By Nilpo


       · In the second installment of this series, I show readers some of the more advanced...
     

    WINDOWS SCRIPTING ARTICLES

    - 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...
    - Modifying XML Files in WSH
    - Reading XML Files in WSH
    - Visual Basic 2005 XML Programming Using XML ...
    - Creating an XML Document in WSH
    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT