Beginning Word Programming In VB

Tuesday Nov 19th 2002 by Ronald Garlit
Share:

This article contains the very basics of programming MS Word (an Automation Server) from an Automation Controller written in Visual Basic 6. Ron Garlit reveals all...

This article contains the very basics of programming MS Word (an Automation Server) from an Automation Controller written in Visual Basic 6. This code will work with MS Word 8.0/9.0 Object Models. It is meant to lead a novice programmer through the basics of creating an Instance of MS Word 97 or 2000. Then adding documents and learning how to call them individually in code with the Range Objects.

The code in the project is loaded with comments and is intended to be stepped through in debug mode with the F8 key in Visual Basic. After each step you should click on the title bar of Word or VB6 to give it focus. DO NOT click in the document area of and instances of MS Word. Before we start look closely at the General Declarations Section of the CreateADoc.frm. You will find the variables and a procedure called IsProgramRunningWord( ). The body of the code looks like this:

If objWord Is Nothing Then  '  ***********************************************  '  Create new Word instance if word isn't running  '  ***********************************************  Set objWord = New Word.ApplicationElse  '  ***********************************************  '  Use an existing instance created by our program  '  ***********************************************  Set objWord = GetObject(, "Word.Application")End If    '}-> If objWord Is Nothing Then

Basically if ObjWord has a value of nothing then we create a new instance. If not we use the GetObject( ) procedure in VB to get a handle of an open iinstance of Word. I cant explain here now. But depending on the version of Word (97/2000) and Windows 95, 98, or W2K you can end up with Multiple instances of MS Word. That is another story when the dust settles and the secrets are weeded out.

After objWord is set up and a instance of Word is established we will proceed to the cmdCreateADoc_Click event. This is where you should set you breakpoint on the first line of the second WITH statement. This is the upper level call to the Application Object of MS Word we created. We will be using WITH statements for several reasons. The most important are speed and good coding form. The speed comes from the fact that we dont have re-enter the application object as we nest into the instance of MS Word.

In a nutshell, these two lines of code are equal to leaving the store each time you buy something and going back in again to purchase another item.

For example:

ObjStore.PickItem.PayforItem.LeaveStoreObjStore.PickItem.PayforItem.LeaveStoreObjStore.PickItem.PayforItem.LeaveStore

This is better and easier to read:

With ObjStore.PickItem.PickItem.PickItem.PayforItem.PayforItem.PayforItem.LeaveStoreEnd With

What would you rather do?

First we enter our Instance of Word.

With objWord

The first line is a call to the Documents collection object with the Add Method. This creates a blank document in MS Word. It automatically adds it to the documents collection.

.Documents.Add

Now we could just use the ActiveDocument for this whole program. But that would be bad form and there is a problem. First you could think of ActiveDocument as being Words own little ME Keyword. But every instance of Word has a ActiveDocument. Plus the instances have to share the document collection information. Word is an Automation Server (is that old lingo now?) It shares space in the registry and all kinds of other goodies mysterious and hidden. This is how you can open one copy of word and then another and one will have Document1 and the other Document2 and so on. Sound like fun yet. Hope we arent scaring you. So we are going to use Call by Name when we can and variables that reference our objects for control, clarity and speed.

Here we created a string variable to hold the name of the document we just created with the Add method.

strWorkingDocName = .ActiveDocument.Name

Next we set a reference to the document in the document variable aThatDoc by calling the document BY NAME (the one in the string variable).

Set aThatDoc = .Documents(strWorkingDocName)

We create another document and repeat the process again for the aThisDoc variable.

.Documents.AddstrWorkingDocName = .ActiveDocument.NameSet aThisDoc = .Documents(strWorkingDocName)

Next we set the range each of the documents in the two range variables. Your wondering what is a Range? The most important object in the Word Object Model in my opinion is the Range Object. Many people make the wrong assumption that it is the Selection Object. This is because when people start to learn VBA they use the Macro recorder to capture an operation they performed in the application and studies that. I still use that method when trying to discover something that is not well documented.

But Microsoft prefers you use the Range Object. Its faster and more powerful and you can have more than one. There are some things you still need the Selection Object for, but that is another story. The Range Object consists of some 73 Properties and 66 Methods. But that is a whole series of articles. Right now this is just a taste of the basics.

Here we are establishing two range variables. Each is set to the starting range of different document.

Set range1 = aThisDoc.RangeSet range2 = aThatDoc.Range

Note the InsertAfter Method then inserts text after the end of the range. When it started it was basically set at the first character position of the document. (Where the insertion cursor is.)

range1.InsertAfter "This is the range object that was just assign the range of " _  & aThisDoc.Namerange2.InsertAfter "This is the range object that was just assign the range of " _ & aThatDoc.Name

Then we use the range1 variable with the Select Method to highlight the Range. Note the insertion cursor in Word.

range1.Select

Next we Collapse the start of the range1 so it equals the end of range1 that was highlighted by the Select Method.

range1.Collapse wdCollapseEnd

Then we use Select again on range1 to highlight the new range so we can see it.

range1.Select

Lets insert some more text.

range1.InsertAfter "We just collapsed to the end of Range1.  That means we moved an invisible cursor to the end of sentence."

End the WITH statement and we are done.

End With    '}-> With objWord

You should print this out and follow it in the debug mode while running the code from the breakpoint we set at the beginning of the WITH statement. All will be come clear.

You will want to look in the help for all these Range Objects and check out the examples and syntax. The range object is the key object of all the MS Offices larger object models.

I hope this inspires you to explore and play with the different properties and methods. Keep in mind that the Range Object (and Selection Object) are used to specify areas in a document such as Bookmarks, Tables, Cells, Hyperlinks, Endnote, Footnote, etc, etc. etc. The list just goes on and on just in Word alone. So dont stay up too late.

Download the files!

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved