Windows Scripting
  Home arrow Windows Scripting arrow 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  
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

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


    (Page 1 of 5 )

    In my last article, I showed you how to connect to Microsoft Word’s OLE automation object to open and create documents. You also learned how to edit and save them. In today’s article, I’m going to take things a bit further and show you some scripting techniques that can be used to perform some more advanced functions.

    We’ll begin with one of the most popular requests I receive.  I’m constantly being asked how to read text from a Word document.  Well, you’re in luck because I’m going to show you how—and it’s actually pretty simple.

    To get started, you’ll either need to open a document or create a new one and fill it with some text.  You learned how to do both in my last article so I won’t go into too much detail.

    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

    What I’ve done here is created an instance of the Word automation object, set some basic properties, and opened an existing Word document.  In this example, I’ve opened the document as read-only to prevent accidentally saving any changes.

    Set objRange = objDoc.Content

    strContents = objWord.CleanString(objRange.Text)

    Next, I set a range that includes the entire contents of the document using the Document object’s Content property.  That Range object has a Text property that returns a text string containing the text of that range.  I’ve gone a little further by using the Word object’s CleanString function as well.  This function will remove any Word-specific formatting from the string.

    objWord.Quit wdDoNotSaveChanges

     

    MsgBox strContents

    I finish up by closing the Word application and displaying the contents in a message box.  Of course, you could use this text string in any way you like.

    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

    - 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 3 Hosted by Hostway
    Stay green...Green IT