The Selection Object





The Selection Object

The Selection object always references one of two things:

  • The selected text.

  • The position of the insertion point cursor.

Because much of what you do in Word involves one or the other of these items (formatting text, inserting text at the insertion point, and so on), the Selection object is one of the most important in Word. (I'm simplifying things a bit for this discussion because the Selection object can also refer to a selected shape, inline shape, or block. I'll deal only with text-related selections in this section.)

You reference the currently selected text or insertion point by using the Selection property without an object qualifier. For example, the following statement formats the selected text as bold:


Selection.Range.Bold = True

To specify a Selection object, use the Select method, which is available with a number of Word objects, including Document, Range, Bookmark, and Table. For example, the following statement selects the first paragraph in the active document:


ActiveDocument.Paragraphs(1).Range.Select

Checking the Selection Type

The Selection object has a number of properties, including many that you've seen already with the Document and Range objects. The few properties that are unique to the Selection object aren't particularly useful, so I won't discuss them here. The lone exception is the Type property:

Type— Returns the type of selection:

wdNoSelection

Nothing is selected.

wdSelectionColumn

A column in a table is selected.

wdSelectionIP

The selection is the insertion point.

wdSelectionNormal

Some text is selected.

wdSelectionRow

A row in a table is selected.

Selection Object Methods

Again, theSelection object offers many of the same methods as the Range object, including Copy, Cut, Delete, InsertAfter, InsertBefore, InsertParagraphAfter, InsertParagraphBefore, and Paste. Here's a look at a few methods that are either unique to the Selection object or are most often used with this object:

Collapse— Removes the selection and positions the insertion point according to the following syntax:


Selection.Collapse Direction

Direction

(optional) Specifies where you want the insertion point to end up. Use wdCollapseStart to position the cursor at the beginning of the Selection (this is the default). Use wdCollapseEnd to position the cursor at the end of the Selection.

EndKey— Extends the selection or moves the insertion point to the end of a specified unit (such as a line). In other words, this method is equivalent to pressing the End key. Note, too, that this method returns the number of characters that the selection was moved. Here's the syntax:


Selection.EndKey(Unit, Extend)

Unit

(optional) Specifies what the Selection is moved to the end of. For regular text, use wdLine (this is the default) or wdStory. In a table, use wdColumn or wdRow.

Extend

(optional) Specifies what happens to the Selection. To extend the selection to the end of the specified Unit, use wdExtend; to collapse the Selection to the insertion point, use wdMove (this is the default).

EndOf— Extends the selection or moves the insertion point to the end of a specified unit (such as a paragraph). This method returns the number of characters that the selection was moved. This method offers a bit more flexibility than the EndKey method. Here's the syntax:


Selection.EndOf(Unit, Extend)

Unit

(optional) Specifies the unit by which the Selection is moved. For regular text, use wdCharacter, wdWord (this is the default), wdLine, wdSentence, wdParagraph, wdSection, or wdStory. In a table, use wdCell, wdColumn, wdRow, or wdTable.

Extend

(optional) Specifies what happens to the Selection. To extend the selection to the end of the specified Unit, use wdExtend; to collapse the Selection to the insertion point at then end of the unit, use wdMove (this is the default).

Expand— Expands the selection using the following syntax:


Selection.Expand Unit

Unit

(optional) Specifies how the Selection is expanded. For regular text, use wdCharacter, wdWord (this is the default), wdLine, wdSentence, wdParagraph, wdSection, or wdStory. In a table, use wdCell, wdColumn, wdRow, or wdTable.

For example, the following code selects the first word in the first paragraph. With the Selection object in place, it then changes the first word to uppercase, expands the selection to the entire paragraph, formats the new selection as bold, and then collapses the selection to put the insertion point after the paragraph:


ActiveDocument.Paragraphs(1).Range.Words(1).Select

With Selection

    .Range.Case = wdUpperCase

    .Expand wdParagraph

    .Range.Bold = True

    .Collapse wdCollapseEnd

End With

HomeKey— Extends the selection or moves the insertion point to the beginning of a specified unit (such as a line). This method is equivalent to pressing the Home key. As with the EndKey method, HomeKey returns the number of characters that the selection was moved. Use the following syntax:


Selection.Home(Unit, Extend)

Unit

(optional) Specifies what the Selection is moved to the beginning of. For regular text, use wdLine (this is the default) or wdStory. In a table, use wdColumn or wdRow.

Extend

(optional) Specifies what happens to the selection. To extend the Selection to the beginning of the specified Unit, use wdExtend; to collapse the selection to the insertion point, use wdMove (this is the default).

Move— Collapses the current selection and moves the insertion point by a specified number of units. This method returns the number of units that the insertion point was moved. Here's the syntax:


Selection.Move(Unit, Count)

Unit

(optional) Specifies the unit by which the insertion point is moved. For regular text, use wdCharacter (this is the default), wdWord, wdLine, wdSentence, wdParagraph, wdSection, or wdStory. In a table, use wdCell, wdColumn, wdRow, or wdTable.

Count

(optional) The number of units by which the insertion point is moved (the default is 1).

graphics/note_icon.gif

If you don't want the current selection deleted when you insert a new paragraph, use the InsertParagraphAfter or InsertParagraphBefore methods. See "Some Useful Range Object Methods," earlier in this chapter.


StartOf— Extends the selection or moves the insertion point to the start of a specified unit (such as a paragraph). This method returns the number of characters that the selection was moved. This method offers a bit more flexibility than the HomeKey method. Here's the syntax:


Selection.StartOf(Unit, Extend)

Unit

(optional) Specifies the unit by which the Selection is moved. For regular text, use wdCharacter, wdWord (this is the default), wdLine, wdSentence, wdParagraph, wdSection, or wdStory. In a table, use wdCell, wdColumn, wdRow, or wdTable.

Extend

(optional) Specifies what happens to the Selection. To extend the selection to the start of the specified Unit, use wdExtend; to collapse the Selection to the insertion point at the start of the unit, use wdMove (this is the default).

TypeBackspace— Deletes the character to the left of the insertion point. If the current selection isn't collapsed, this method deletes the selected text. (This method is equivalent to pressing the Backspace key.)

TypeParagraph— Inserts a new paragraph at the insertion point. If the current selection isn't collapsed, this method deletes the selected text. (This method is equivalent to pressing the Enter key.)

TypeText— Inserts text at the insertion point. If the current selection isn't collapsed and the Options.ReplaceSelection property is True, this method replaces the selection with the specified text. Here's the syntax:


Selection.TypeText Text

Text

The string to be inserted.

Listing 7.4 shows a procedure that inserts text. It first checks to see whether the current selection is the insertion point or whether Options.ReplaceSelection is False. If either is true, the text is inserted. Otherwise, the user is asked if they want to replace the selected text. (See Chapter 12, "Interacting with the User," to learn about the MsgBox function.)

A Procedure that Asks the User if the Selected Text Should Be Replaced

Sub AskToReplaceText()

    Dim result As Integer

    '

    ' Are we at the insertion point or is ReplaceSelection off?

    '

    If Selection.Type = wdSelectionIP Or _

       Not Options.ReplaceSelection Then

       '

       ' If so, just insert the text

       '

       Selection.TypeText "Howdy"

    Else

        '

        ' Otherwise, ask the user about replacing the text

        '

        result = MsgBox("Replace the selected text?", vbYesNo + vbQuestion)

        If result = vbNo Then

            '

            ' Turn off ReplaceSelection to insert without replacing

            '

            Options.ReplaceSelection = False

            Selection.TypeText "Howdy"

            Options.ReplaceSelection = True

        Else

            '

            ' Replace the selected text

            '

            Selection.TypeText "Howdy"

        End If

    End If

End Sub


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows