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

                          



Represents the selection in a window pane. The selection can either encompass an area in the document or be collapsed to an insertion point.

Note   There can only be one Selection object per document window pane and only one Selection object can be active.

Using the Selection Object

Use the Selection property to return the Selection object. The following example collapses the selection (if necessary) and moves the insertion point to the end of the current line.

Selection.EndKey Unit:=wdLine, Extend:=wdMove

The following example updates the results of the fields in the selection.

If Selection.Fields.Count >= 1 Then Selection.Fields.Update

Use the Type property to return the selection type (for example, a block selection or an insertion point). The following example selects the current paragraph if the selection is an insertion point.

If Selection.Type = wdSelectionIP Then
    Selection.Paragraphs(1).Range.Select
End If

Use the Information property to return information about the selection. If the selection is in a table, the following example displays the number or rows and columns in the table.

If Selection.Information(wdWithInTable) = True Then
    MsgBox "Columns = " _
        & Selection.Information(wdMaximumNumberOfColumns) _
        & vbCr & "Rows = " _
        & Selection.Information(wdMaximumNumberOfRows)
End If

Use the Select method to select an item in a document. The following example selects the first bookmark in the active document and formats it to appear in red.

If ActiveDocument.Bookmarks.Count >= 1 Then
    ActiveDocument.Bookmarks(1).Select
    Selection.Font.ColorIndex = wdRed
End If

The Selection object also includes various methods you can use to expand or move an existing selection. For example, the MoveDown method has an Extend argument that you can set to wdExtend. The following example selects the next three paragraphs in the active window.

With Selection
    .StartOf Unit:=wdParagraph, Extend:=wdMove
    .MoveDown Unit:=wdParagraph, Count:=3, Extend:=wdExtend
End With

Remarks

Use the Range property to return a Range object from the Selection object. The following example defines the variable myRange as the selected range.

Set myRange = Selection.Range

When you record macros, the macro recorder will often record changes to the Selection object. The following recorded macro applies bold formatting to the first two words in the document, and then inserts a new paragraph.

Selection.HomeKey Unit:=wdStory
Selection.MoveRight Unit:=wdWord, Count:=2, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph

The following example accomplishes the same task as the preceding example, but without using the Selection object.

Set myRange = ActiveDocument.Range(Start:=0, _
    End:=ActiveDocument.Words(2).End)
myRange.Bold = True
myRange.InsertParagraphAfter

There can be only one Selection object per window pane; however, you can have multiple Range objects defined in a single document. A Range object represents an document area that may or may not be selected. Working with Range objects, you can manipulate a document with minimal screen updates. For more information, see Working with Range objects.