O'Reilly logo

Writing Word Macros, Second Edition by Steven Roman PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

Working with two selections

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required