[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Using the Word Range object in code

Published on
13,678 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

LVL 61

Expert Comment


Nice article - voted 'Yes' above.

Expert Comment

by:Varun Walia
Learned a lot, but in a few case may be some contradiction here??

Will surely let you know if problem persist.

Thanks again.

Featured Post

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Join & Write a Comment

In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month