Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - Scripting Microsoft Word
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

Scripting Microsoft Word
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-10-06

    Table of Contents:
  • Scripting Microsoft Word
  • Creating and Opening Documents
  • Editing and Formatting Documents
  • Closing, Saving, and Exiting

  • 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


    Scripting Microsoft Word - Creating and Opening Documents


    (Page 2 of 4 )

    At this point, the Word application is running but we don’t have any open documents to work with.  I’ll show you how to create a new document, open an existing one, and switch back and forth between multiple open documents.  Let’s begin by creating a new document.

    Set objNewDoc = objWord.Documents.Add

    Here I’ve created a new document based on the Normal template.  The most effective way to create a new document is by using the Add method of the Documents collection.  The Documents collection is a collection of Document objects that represents all open documents.

    Documents.Add(Template, NewTemplate, NewDocumentType, Visible)

    You may provide optional attributes to the Add method.  Template is a string that indicates a path to a document template to use.  If omitted, the default Normal template is used.  NewTemplate is a Boolean value that indicates whether or not to open the new document as a template.  NewDocumentType accepts any one of the wdNewDocumentType constants listed in the table below.  If omitted, the default wdNewBlankDocument (0) is used.  Visible is a Boolean value that indicates whether or not the document should be opened in a visible window.  This defaults to True but will be overridden if the Word object itself has visibility set to False.

    wdNewDocumentType Constants

    wdNewBlankDocument = 0

    wdNewWebPage = 1

    wdNewEmailMessage = 2

    wdNewFrameset = 3

    wdNewXMLDocument = 4

    The following example uses the Documents.Add method to open a new document using the Professional Memo template.

    strTemplate = "C:Program FilesMicrosoft Office" _

       & "TemplatesMemosProfessional Memo.dot"

    Set objDoc = objWord.Documents.Add(strTemplate)

    The Documents collection object also provides an Open method that you can use to open an existing document.  The Open method provides very extensive syntax; however, it only requires a single attribute that represents the path to the file to be opened.  Each of the attributes may also be set using equivalent properties provided by the Documents object.  I’ll show you both ways and you’ll understand what I mean.

    For brevity’s sake, I won’t be discussing all of the available attributes since most will go unused.  For more information on the Open method and its available attributes, consult the MSDN documentation here.

    Documents.Open(FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument, WritePasswordTemplate, Format, Encoding, Visible, OpenConflictDocument, OpenAndRepair, DocumentDirection, NoEncodingDialog)

    For most purposes, we’ll only be concerned with the first and third attributes.  FileName is a string that represents the path to the document file.  This may be a local path or any fully qualified UNC path to a network file.  You can even use URLs.

    If you are using Jscript, don’t forget to escape any and all forward slashes in your paths!

    The third attribute is optional and accepts a Boolean value indicating whether or not to open the document as Read-Only.  This can be used as an added safety precaution.  For example, you may want to print out a document with some changes without affecting the original document.

    Set objDoc = objWord.Documents.Open("MyDoc.doc",, True)

    The previous example opens the document MyDoc as Read-Only.  Any changes made to the document will not be saved to the original.  We’ll see later how to save a document in case you would like to save the changes to a new document.

    Now that you know how to open new and existing documents, you need to know how to switch back and forth between them.  The Document object provides an Activate method that allows you to specify which document has focus.  The Word Application object also provides a read-only ActiveDocument property that returns a reference to the currently active document.

    Be aware that the ActiveDocument property will result in an error if there are no documents open.  A simple If statement can be used to prevent this.

    In the following example, we’re going to switch between our two open documents.  The newly opened document should have focus already.  We’ll switch back to the new document we created.  I’ll use a message box to indicate that the switch was made.  Notice how I use a simple If statement to check for open documents in order to avoid any errors.  I’m assuming that if there is at least one document open, then both required documents are open.

    If objWord.Documents.Count >= 1 Then

       objNewDoc.Activate

       MsgBox "The " & objNewDoc.Name & " document has been activated. "

    Else

       MsgBox "There are no documents open."

    End If

    Here, we’re using the Activate method to focus on the objNewDoc object.  We’re checking the Count property of the Documents collection to ensure that it contains references to open documents.

    The Word Application object also provides an Activate method that can be used to switch between multiple running instances of the Word application!

    More Windows Scripting Articles
    More By Nilpo


       · In this article, we look at scripting the Microsoft's Word Automation Object for...
       · Thank you for your outstanding article. It is very well written, practical, and...
       · Thank you so much for your kind words. I'm very glad that you found this article...
     

    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
    Stay green...Green IT