Using the Word Range object in code

Published on
13,589 Points
6 Endorsements
Last Modified:
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

1 Comment
LVL 61

Expert Comment


Nice article - voted 'Yes' above.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Join & Write a Comment

This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month