The Selection Object vs. the Range Object
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
In many ways, the Selection object is similar to a Range object. The Selection object represents an arbitrary portion of a document. It has properties that represent characters, words, sentences, paragraphs, and other objects in a Word document. The main difference is that when you use the Range object, it's not necessary to first select the text. In addition, there can only be one Selection object at a time, but the number of Range objects you can create is unlimited.
The Selection object and the Range object have many common methods and properties, and it is easy to return a Range object from a Selection object or to create a Selection object from a Range object. However, most things you can do with a Selection object, you can do even faster with a Range object. There are two main reasons for this:
- The Range object typically requires fewer lines of code to accomplish a task.
- Manipulating a Range object does not incur the overhead associated with Word having to move or change the selection "highlight" in the active document.
In addition, you can do much more with a Range object than you can with a Selection object:
- You can manipulate a Range object without changing what the user has selected in the document. Practically speaking, you could save the original selection by using a Range object variable, manipulate the Selection object programmatically, and then use the saved Range object's Select method to display the original selection, but there is rarely a good reason to show the user that the selection is changing. Some WordBasic developers relied on changing the selection to indicate to the user that the code is still running (and the machine has not locked up). But this is not the right way to convey information to a user. An operation that takes a long time to execute should signal its progress by using a progress meter or by posting status messages to the status bar.
- You can maintain multiple Range objects in your code, and, where necessary, store those objects in a custom Collection object. You cannot do these two things by using only the Selection object.
When it comes to manipulating text, the Selection and Range objects have many methods and properties in common — for example, all the InsertName methods and the Text property discussed in "Working with Text in a Range Object". However, the Selection object has a unique set of methods for manipulating text. These are the TypeText, TypeParagraph, and TypeBackspace methods. You use these methods to enter or remove text and insert paragraph marks in a Selection object. To get the results you expect, there are a few things you must understand about the TypeName methods.
With the exception of the InsertParagraph and InsertFile methods, which remove selected text, the InsertName methods let you work with a selection without deleting existing text. In contrast, the TypeName methods might delete existing text, depending on the value of the Options object's ReplaceSelection property.
When the ReplaceSelection property is True, using any of the TypeName methods results in the currently selected text being replaced. When the ReplaceSelection property is False, the TypeText and TypeParagraph methods behave just as the InsertBefore method: The text or paragraph mark is inserted at the beginning of the current selection. When the ReplaceSelection property is False, the TypeBackspace method behaves the same as the Collapse method of a Range or Selection object when the wdCollapseStart constant is specified in the Direction argument. The Collapse method collapses a range or selection so that its starting point and ending point are the same.
Working with Microsoft Word Objects | Working with Document Content | The Range Object | The Selection Object | Working with Bookmarks | The Find and Replacement Objects