March 8, 2021
Hot Topics:

Beginning Word Programming In VB

  • By Ronald Garlit
  • Send Email »
  • More Articles »

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.


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.


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

Page 5 of 6

This article was originally published on November 20, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date