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

Scripting Microsoft Word
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    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 - Editing and Formatting Documents


    (Page 3 of 4 )

    We’re now going to explore a few ways to edit documents.  Please note that this section is only a very brief introduction.  You will most likely need to do some experimentation with your own documents since editing is highly dependent upon the styles and formatting that exist within the document.

    To begin, we’re going to assume that we’re working with our newly created blank document.  We’ll first want to add a couple of paragraphs.

    Set objSelection = objWord.Selection

    We begin by selecting the contents of your document.  In our new document, we’re simply creating an empty selection but we’ll need that selection reference to type our paragraphs.

    objSelection.Font.Name = "Arial"

    objSelection.Font.Size = "10"

    objSelection.TypeText "=rand(1,5)"

    objSelection.TypeParagraph()

     

    objSelection.Font.Size = "12"

    objSelection.TypeText "=rand(1,5)"

    objSelection.TypeParagraph()

    Adding text is quite simple.  I’m using a few selection properties to control some basic formatting such as font face and size, and then typing the text to be inserted using the TypeText method.  The TypeParagraph method is used to insert paragraph breaks between my text samples.

    The TypeText method accepts a single attribute that should be the literal text to be included.  Since I’m lazy (and I didn’t want to clog up my code with a bunch of lengthy paragraphs) I’ve used a little known Word function to insert some random sample text for me.

    Depending on your Word version and regional settings, your document should now contain two paragraphs that look similar to the following:

    On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look. You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab.

     

    On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your current document look. You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab.

    If you’re using a Word version older than Word 2007 you will probably see something more like this:

    The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

     

    The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

    In any case, we have a few sample paragraphs to work with now.  Let’s take a look at how we can append paragraphs to a Word document and how to insert them between other paragraphs.  To do both of these, we’ll need to change our selection.

    Const wdStory = 6

    Const wdMove = 0

     

    Set objSelection = objWord.Selection

    objSelection.EndKey wdStory, wdMove

     

    objSelection.Font.Size = "14"

    objSelection.Font.Bold = True

    objSelection.TypeText "=lorem(1,5)"

    objSelection.TypeParagraph()

    The code sample above will append a paragraph to the end of our document.  Notice how I’ve reset my selection before changing anything.  This ensures that the entire document's contents are selected.

    Next I use the EndKey method.  This method moves or extends a selection from the end of the specified unit.  I’m telling Word to move my selection to the end of the entire story (the full document contents).  I now have an empty selection immediately following the document's contents.  Here, I just write a new paragraph as before.

    Set objSelection = objWord.Selection

    objSelection.HomeKey wdStory, wdMove

     

    objSelection.Font.Size = "14"

    objSelection.Font.Bold = True

    objSelection.TypeText "=lorem(1,5)"

    objSelection.TypeParagraph()

    I use the same procedure to insert a paragraph at the beginning of my document as well.  The only difference is that I’m using the HomeKey method instead of the EndKey method.  This moves my selection to the beginning instead of the end.

    Const wdParagraph = 4

     

    Set objSelection = objWord.Selection.Paragraphs(2)

    objSelection.EndKey wdParagraph, wdMove

     

    objSelection.Font.Size = "14"

    objSelection.Font.Bold = True

    objSelection.TypeText "=lorem(1,5)"

    objSelection.TypeParagraph()

    To insert paragraphs we use the same two procedures.  We can iterate through the paragraphs collection to select single paragraphs within the document.  Once we have a single paragraph selected, we can simply type a new paragraph either before or after it.  The code sample above inserts a paragraph between our two original paragraphs.

    I’ve done this by selecting the second paragraph in the document and then inserting a paragraph after it.  Notice that I’m now using the wdParagraph unit size with the EndKey method because I want to move to the end of the current paragraph rather than the end of the current document.  You can view all of the available unit size constants in the MSDN documentation here.

    More Windows Scripting Articles
    More By Nilpo


       · In this article, we look at scripting the Microsoft's Word Automation Object for...
     

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