Thursday, February 4, 2010

Ms. Word Automation

How to automate MS. Word by using VB6?



Sometimes, we want to create a word document programmatically, such as for reports. This task can be easily done by using the automation feature in Ms. Word.

The term of automation (in this article) means the process of controlling a program, in this case: MS. Word,  from another program.

So, how do we control the MS. Word through VB6? Read on...

First Tutorial - Hello World

  1. Create a new Standard EXE Project
  2. Add Ms. Word references to Ms. Word object library.
    Click Project > Reference. Pick Microsoft Word XX.X Object Library and Microsoft Office XX.X Object Library from the References dialog.

    Notice that XX.X may vary, depending on the version of the Ms. Word or Ms. Office installed on your computer.
    Microsoft Word XX.X Object Library is the chore library in automating the Ms. Word, including access to all Word Object Model (document, tables, paragraphs, etc).
    But, Microsoft Office XX.X Object Library provide commons library for Ms. Office application (Ms. Word), including data types which often needed in using Microsoft Word XX.X Object Library.

    Now, after you add those references, you will see the 2 new entries in the Object Browser (press F2). They are Word (for Microsoft Word XX.X Object Library) and Office (for Microsoft Office XX.X Object Library).
  3. Open the code view and paste this code in the Form_Load Sub (Event).

    Dim oWord As Word.Application
    Dim owDoc As Word.Document

    Set oWord = New Word.Application
    oWord.Visible = True
    Set owDoc = oWord.Documents.Add

  4. Click Start in the toolbar or press F5 to Run the program.
    Now you will see a new (blank) document Ms. Word appeared as soon as the program start.

Code Explanation
This is just simple code. All statements are self-defined.

Dim oWord As Word.Application

First we declare a variable for handling the Application object. This Aplication is the main object needed in every automation program. Think of it as the Ms. Word itself with no document opened.

Dim owDoc As Word.Document

Next we declare a Document object. By using this object, we get access to manipulate (Automate) the Ms.Word Document, such as add text, praragraph, table, picture, etc.

Set oWord = New Word.Application

Here we instantiate (creating new object of) the Application. You may also use CreateObject
or GetObject method.

Then, we make the Application appear using this code.

oWord.Visible = True

Finally, we create a new Document and add it to the main Application.

Set owDoc = oWord.Documents.Add


Finishing
Now to finish this tutorial, we will add some more statements, so that the code will be.

Option Explicit

Private Sub Form_Load()
Dim oWord As Word.Application
Dim owDoc As Word.Document

Set oWord = New Word.Application
oWord.Visible = True
Set owDoc = oWord.Documents.Add

Dim owPara As Word.Paragraph

Set owPara = owDoc.Content.Paragraphs.Add()
owPara.Range.Text = "HELLO WORLD"
owPara.Range.Font.Bold = True
owPara.Format.SpaceAfter = 24
owPara.Range.InsertParagraphAfter

Set owPara = owDoc.Content.Paragraphs.Add()
owPara.Range.Text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
owPara.Range.Font.Bold = False
owPara.Range.Font.Italic = True
owPara.Range.InsertParagraphAfter

Dim owTable As Word.Table
Set owTable = owDoc.Content.Tables.Add(owDoc.Bookmarks("\endofdoc").Range, 3, 2)
owTable.Range.Font.Bold = False
owTable.Range.Font.Italic = False
owTable.Borders.OutsideLineStyle = wdLineStyleDouble
owTable.Borders.InsideLineStyle = wdLineStyleSingle
owTable.Cell(1, 1).Range.Text = "Cell (1, 1)"
owTable.Cell(1, 2).Range.Text = "Cell (1, 2)"
owTable.Cell(2, 1).Range.Text = "Cell (2, 1)"
owTable.Cell(2, 2).Range.Text = "Cell (2, 2)"
owTable.Cell(3, 1).Range.Text = "Cell (3, 1)"
owTable.Cell(3, 2).Range.Text = "Cell (3, 2)"

End Sub

Pheww....Now try to run the program. You will see some texts appeared in the Document.

Code Explanation
Actually, the code is still self-defined.
To add some text we need to access the Content object of Document. In the example, we use Content object to add two paragraphs and a table.
Another thing to be noticed, that the most of the code using something called Range object. This object specifying the range of any objects in the Content.

owPara.Range.InsertParagraphAfter is simply tell the Document, we want to add a paragraph after this current Paragraph.

owDoc.Bookmarks("\endofdoc").Range is used to obtaining the Range object located in certain position in the Document, which is specified by the string "\endofdoc".

What's Next
Here, you see how much code you need just to add Text to the Document object. However, that's not the only way.
For the next tutorial, I will show you a very simple way to do it, and some more interesting stuffs.

Learning the Object Model provided by Ms. Word will make great asset in understanding Ms. Word Automation.

No comments:

Post a Comment