Selection Object

Description

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.

Properties

Active property, Application property, BookmarkID property, Bookmarks property, Borders property, Cells property, Characters property, Columns property, ColumnSelectMode property, Comments property, Creator property, Document property, End property, Endnotes property, ExtendMode property, Fields property, Find property, Flags property, Font property, Footnotes property, FormattedText property, FormFields property, Frames property, HeaderFooter property, Hyperlinks property, Information property, InlineShapes property, IPAtEndOfLine property, IsEndOfRowMark property, LanguageID property, LanguageIDFarEast property, LanguageIDOther property, Orientation property, PageSetup property, ParagraphFormat property, Paragraphs property, Parent property, PreviousBookmarkID property, Range property, Rows property, Sections property, Sentences property, Shading property, ShapeRange property, Start property, StartIsActive property, StoryLength property, StoryType property, Style property, Tables property, Text property, Type property, Words property.

Methods

Calculate method, Collapse method, ConvertToTable method, Copy method, CopyAsPicture method, CopyFormat method, CreateTextbox method, Cut method, Delete method, EndKey method, EndOf method, EscapeKey method, Expand method, Extend method, GoTo method, GoToNext method, GoToPrevious method, HomeKey method, InRange method, InsertAfter method, InsertBefore method, InsertBreak method, InsertCaption method, InsertCells method, InsertColumns method, InsertCrossReference method, InsertDateTime method, InsertFile method, InsertFormula method, InsertParagraph method, InsertParagraphAfter method, InsertParagraphBefore method, InsertRows method, InsertSymbol method, InStory method, IsEqual method, Move method, MoveDown method, MoveEnd method, MoveEndUntil method, MoveEndWhile method, MoveLeft method, MoveRight method, MoveStart method, MoveStartUntil method, MoveStartWhile method, MoveUntil method, MoveUp method, MoveWhile method, Next method, NextField method, NextRevision method, NextSubdocument method, Paste method, PasteFormat method, PasteSpecial method, Previous method, PreviousField method, PreviousRevision method, PreviousSubdocument method, Select method, SelectColumn method, SelectCurrentAlignment method, SelectCurrentColor method, SelectCurrentFont method, SelectCurrentIndent method, SelectCurrentSpacing method, SelectCurrentTabs method, SelectRow method, SetRange method, Shrink method, Sort method, SortAscending method, SortDescending method, SplitTable method, StartOf method, TypeBackspace method, TypeParagraph method, TypeText method, WholeStory method.

See Also

Range object.