Creating a Range or Selection Object
There are many ways to create a Range object. Indeed, there are no less than 74 properties and methods of various Word objects that return a Range object, and I discuss several of them shortly.
The procedure for creating a Selection object is a bit different, however. First, I use the Select method to select an item (text, bookmark, field, and so on). Then I use the Selection property to return a Selection object that represents that item. For instance, the code:
ActiveDocument.Select Set sel = ActiveDocument.ActiveWindow.Selection
first selects the entire document and then sets the variable sel
to refer to the current selection in the active document (that is, the entire document). As another example, the code:
ActiveDocument.Fields(2).Select ActiveDocument.ActiveWindow.Selection.Delete
will select and then delete the second Field object in the active document.
Figure 14-3. Working with two selections
The Selection property applies only to the Application object, the Pane object and the Window object. However, there are many objects that have a Select method:
Bookmark |
Frame |
Rows |
Cell |
InlineShape |
Selection |
Column |
MailMergeField |
Shape |
Columns |
OLEControl |
ShapeRange |
Document |
PageNumber |
Subdocuments |
Field |
Range |
Table |
FormField |
Row |
The Selection property is a global property, so it can be referenced without specifying a parent. For instance, ...
Get Writing Word Macros, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.