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:
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.
Our community of experts have been thoroughly vetted for their expertise and industry experience. Experts with Gold status have received one of our highest-level Expert Awards, which recognize experts for their valuable contributions.
Comments (2)
Commented:
Nice article - voted 'Yes' above.
Commented:
Will surely let you know if problem persist.
Thanks again.