Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

Using the Word Range object in code

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing.

A Range property can define any part of a Word document, so has beginning and an end, which are simply character counts. Not all ranges are in the body of the document. There are separate sets of ranges (called Stories) for other parts such as headers, footers, comments, etc, which won't show in the body range.

The advantages of using a Range object in your code are several.

It is faster, because it doesn't necessarily have to wait for the screen to catch up. Screen flicker is also minimised.

The user's selection will remain unchanged (or close to the point if its range gets deleted).

You can have as many ranges as you like in your code, while there is only one Selection object. For instance you can deal in the code with several documents, without having to make each one active when you want to work on it.

You can keep track of the text and change the formatting after changing it:
MyRange.Text = “Some text”
                      MyRange.Font.Bold = True

Open in new window

Not altogether a specific the Selection object problem but you may notice that recorded macros are very long. This is because all the defaults are coded as if you specifically wanted to set them to the default. However in Find recordings, you will notice a ClearFormatting. It is only one extra line, but it illustrates the fact that because there is only one Selection object per application instance, its settings are kept between each usage, including that by the document editor via the Word user interface  

A Range object is independent of the pagination and line folding. Selection methods such as MoveDown could give unexpected results if the text has been changed in the document before the selection point, or the different printer driver different from the one in use when the macro was recorded or written.

The last fact is means the code must be more specific. Instead of MoveDown, MoveLeft, MoveLeft, the code would have to look for a particular word or phrase (perhaps, though not necessarily, using the Find object). Novice users will find this a nuisance, but it has a couple of more subtle advantages. Firstly the code will be more comprehensible (hence more maintainable) and secondly the coder will rapidly come to understand the Word object model and will be able to write their code better and more quickly.

There are some things that can’t be done easily with a range object. They are generally those that depending on pagination, which is almost literally the last thing that Word is concerned with. There are no specific pages in a Word document file. Pagination only happens when the document is opened and Word can format for the current printer driver.

For instance:
MyRange.GoTo wdGoToPage, wdGoToAbsolute, 10

Open in new window

This code is accepted and apparently executed, but nothing happens.

You would have to use the Selection object instead:
                      Selection.GoTo wdGoToPage, wdGoToAbsolute, 10
                      Set MyRange = Selection.Range

Open in new window


Comments (2)

Most Valuable Expert 2012
Top Expert 2013


Nice article - voted 'Yes' above.
Varun WaliaPHP developer

Learned a lot, but in a few case may be some contradiction here??

Will surely let you know if problem persist.

Thanks again.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.