Image of Navigational Map linked to Home / Contents / Search The word is Word

by Matt Gray - GUI Computing
Image of Line Break

Using the Microsoft Word 8.0 Object Library.

If you have ever needed to generate a Word document from a VB app, format this document with alignment, fonts and the like, save it, print it and store it in a database - all via VB code - then 'by Jingo by Crikey' (or whatever) you are reading the right article.

You can do all this and more using the Microsoft Word 8.0 Object Library - which allows you to basically use Word's capabilities in code. I thought this information might be useful to some, considering such a task is tedious, and the object library lacking in documentation. Also it just so happens that this was the requirement of an application we just built.

To code this from VB you obviously need the Microsoft Word 8.0 Object library loaded in your references.

If you want to do stuff like inserting paragraphs, headings, formatted text and page breaks (which is what I needed to do!), then try getting a document to look like this:

My Exciting Heading

This is paragraph one with some bold and some italic.

This is a paragraph with a great url - http://www.gui.com.au/

The code:

'Start a new Word application object
Dim objApp As Word.Application
Set objApp = New Word.Application

'Start a new Normal template document
Documents.Add 

'Format document heading
Selection.Style = ActiveDocument.Styles("Heading 1")
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
With Selection.Font
   .Name = "Times New Roman"
   .AllCaps = True
End With
Selection.TypeText Text:="My Exciting Heading"
Selection.TypeParagraph
Selection.TypeParagraph

'Format body
Selection.Style = ActiveDocument.Styles("Normal")    'Normal for body text
Selection.TypeText Text:="This is paragraph one with some "
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="bold"
Selection.Font.Bold = wdToggle
Selection.TypeText Text:=" and some "
Selection.Font.Italic = wdToggle
Selection.TypeText Text:="italic."
Selection.Font.Italic = wdToggle
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="This is a paragraph with a great url - "
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "http://www.gui.com.au/", SubAddress:=""

'save and print
ActiveDocument.SaveAs "c:\mydoc.doc", wdFormatDocument  'save .doc format
objApp.PrintOut

'close and quit word
Documents.Close
objApp.Quit
Set objApp = Nothing

NB. The Selection,Documents and ActiveDocument properties are global so you don't need to use objApp.Selection or objApp.Documents in code.

The Normal template document is chosen by default when you create a new document - Documents.add - but you can create other templates as well. Document.add takes two optional parameters - Template and NewTemplate. Try looking in your office directory under Templates for Normal.dot (Normal template) and other template files.

The ActiveDocument is of course the document that is active - if you are working with more than one document then you need to activate the document that you want to do things to. Eg. Windows(1).Activate will make the first document active.

Formatting

I found that the best method for generating a document was to format as I go - like I would when producing a document manually in Word. The Selection property as it suggests does its thing to the selected area - so it is easier to format as you go instead of doing it as an afterthought. Eg. Turning bold on and off This is also useful when trying to put page breaks in. I found that going to the end of my text (when formatting in this manner is the end of my document), then placing the break works best:

Selection.EndKey Unit:=wdStory   'go to the end of the document
Selection.InsertBreak Type:=wdSectionBreakNextPage 'place next page break

The ActiveDocument.Saveas command, as in word, can be saved to different formats like .doc (wdFormatDocument), .txt(wdFormatText) or .rtf(wdFormatRTF) - which is useful if you want to save the document into a database memo field.

Another problem I found was the Printout method which can take some time running from your app - but you don't know how long. To wait for this process you might need to find the process with an API and use waitForSingleObject to wait for the printing to finish.

Anyway, I hope this stuff has helped your Word Generation. Even though it's a piece of pie it is tedious and not well documented, so at the least I hope I saved you some thinking time.

If you do find this an interesting topic however, and need to do things not mentioned in this article (i.e. most things!), you can record a macro in Word '97 (Tools - Macro - Record New Macro) , do a lot of cool Word stuff and see what code the macro comes up with - which can usually be helpful in generating what you need. This can open up a whole new world with Word, all thanks to the wonderful commonality of VB.



Written by: Matthew Gray
June '98

Image of Arrow linked to Previous Article Image of Arrow linked to Next Article
Image of Line Break
[HOME] [TABLE OF CONTENTS] [SEARCH]