Share via


Selection Object

Multiple objects
Selection
Multiple objects

Represents the current selection in a window or pane. A selection represents either a selected (or highlighted) area in the document, or it represents the insertion point if nothing in the document is selected. There can only be one Selection object per document window pane, and only one Selection object in the entire application can be active.

Using the Selection Object

Use the Selection property to return the Selection object. If no object qualifier is used with the Selection property, Word returns the selection from the active pane of the active document window. The following example copies the current selection from the active document.

Selection.Copy

The following example cuts the selection from the third document in the Documents collection. The document doesn't have to be active to access its current selection.

Documents(3).ActiveWindow.Selection.Cut

The following example copies the selection from the first pane of the active document and pastes it into the second pane.

ActiveDocument.ActiveWindow.Panes(1).Selection.Copy
ActiveDocument.ActiveWindow.Panes(2).Selection.Paste

The Text property is the default property of the Selection object. Use this property to set or return the text in the current selection. The following example assigns the text in the current selection to the variable strTemp, removing the last character if it is a paragraph mark.

Dim strTemp as String

strTemp = Selection.Text
If Right(strTemp, 1) = vbCr Then _
    strTemp = Left(strTemp, Len(strTemp) - 1)

The Selection object has various methods and properties with which you can collapse, expand, or otherwise change the current selection. The following example moves the insertion point to the end of the document and selects the last three lines.

Selection.EndOf Unit:=wdStory, Extend:=wdMove
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend

The Selection object has various methods and properties with which you can edit selected text in a document. The following example selects the first sentence in the active document and replaces it with a new paragraph.

Options.ReplaceSelection = True
ActiveDocument.Sentences(1).Select
Selection.TypeText "Material below is confidential."
Selection.TypeParagraph

The following example cuts the last paragraph of the first document in the Documents collection and pastes it at the beginning of the second document.

With Documents(1)
    .Paragraphs.Last.Range.Select
    .ActiveWindow.Selection.Cut
End With

With Documents(2).ActiveWindow.Selection
    .StartOf Unit:=wdStory, Extend:=wdMove
    .Paste
End With

The Selection object has various methods and properties with which you can change the formatting of the current selection. The following example changes the font of the current selection from Times New Roman to Tahoma.

If Selection.Font.Name = "Times New Roman" Then _
    Selection.Font.Name = "Tahoma"

Use properties like Flags, Information, and Type to return information about the current selection. You could use the following example in a procedure to determine if there were anything actually selected in the active document; if not, the rest of the procedure would be skipped.

If Selection.Type = wdSelectionIP Then
    MsgBox Prompt:="You haven't selected any text! Exiting procedure..."
    Exit Sub
End If

Remarks

Even when a selection is collapsed to an insertion point, it isn't necessarily empty. For example, the Text property will still return the character to the right of the insertion point; this character also appears in the Characters collection of the Selection object. However, calling methods like Cut or Copy from a collapsed selection will cause an error.

It's possible for the user to select a region in a document that doesn't represent contiguous text (for example, when using the ALT key with the mouse). Because the behavior of such a selection can be unpredictable, you may want to include a step in your code that checks the Type property of a selection before performing any operations on it (Selection.Type = wdSelectionBlock). Similarly, selections that include table cells can also lead to unpredictable behavior. The Information property will tell you if a selection is inside a table (Selection.Information(wdWithinTable) = True). The following example determines if a selection is normal (in other words, it isn't a row or column in a table, it isn't a vertical block of text, and so forth); you could use it to test the current selection before performing any operations on it.

If Selection.Type <> wdSelectionNormal Then
    MsgBox Prompt:="Not a valid selection! Exiting procedure..."
    Exit Sub
End If

Because Range objects share many of the same methods and properties as Selection objects, using Range objects is preferable for manipulating a document when there isn't a reason to physically change the current selection. For more information on Selection and Range objects, see Working with the Selection object and Working with Range objects .

Properties | Active Property | Application Property | BookmarkID Property | Bookmarks Property | Borders Property | Cells Property | Characters Property | ChildShapeRange Property | Columns Property | ColumnSelectMode Property | Comments Property | Creator Property | Document Property | Editors Property | End Property | EndnoteOptions Property | Endnotes Property | EnhMetaFileBits Property | ExtendMode Property | Fields Property | Find Property | FitTextWidth Property | Flags Property | Font Property | FootnoteOptions Property | Footnotes Property | FormattedText Property | FormFields Property | Frames Property | HasChildShapeRange Property | HeaderFooter Property | HTMLDivisions Property | Hyperlinks Property | Information Property | InlineShapes Property | IPAtEndOfLine Property | IsEndOfRowMark Property | LanguageDetected Property | LanguageID Property | LanguageIDFarEast Property | LanguageIDOther Property | NoProofing 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 | SmartTags Property | Start Property | StartIsActive Property | StoryLength Property | StoryType Property | Style Property | Tables Property | Text Property | TopLevelTables Property | Type Property | Words Property | XML Property | XMLNodes Property | XMLParentNode Property

Methods | BoldRun Method | Calculate Method | ClearFormatting Method | Collapse Method | ConvertToTable Method | Copy Method | CopyAsPicture Method | CopyFormat Method | CreateAutoTextEntry Method | CreateTextbox Method | Cut Method | Delete Method | DetectLanguage Method | EndKey Method | EndOf Method | EscapeKey Method | Expand Method | Extend Method | GoTo Method | GoToEditableRange Method | GoToNext Method | GoToPrevious Method | HomeKey Method | InRange Method | InsertAfter Method | InsertBefore Method | InsertBreak Method | InsertCaption Method | InsertCells Method | InsertColumns Method | InsertColumnsRight Method | InsertCrossReference Method | InsertDateTime Method | InsertFile Method | InsertFormula Method | InsertParagraph Method | InsertParagraphAfter Method | InsertParagraphBefore Method | InsertRows Method | InsertRowsAbove Method | InsertRowsBelow Method | InsertStyleSeparator Method | InsertSymbol Method | InsertXML Method | InStory Method | IsEqual Method | ItalicRun Method | LtrPara Method | LtrRun 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 | PasteAndFormat Method | PasteAppendTable Method | PasteAsNestedTable Method | PasteExcelTable Method | PasteFormat Method | PasteSpecial Method | Previous Method | PreviousField Method | PreviousRevision Method | PreviousSubdocument Method | RtlPara Method | RtlRun Method | Select Method | SelectCell Method | SelectColumn Method | SelectCurrentAlignment Method | SelectCurrentColor Method | SelectCurrentFont Method | SelectCurrentIndent Method | SelectCurrentSpacing Method | SelectCurrentTabs Method | SelectRow Method | SetRange Method | Shrink Method | ShrinkDiscontiguousSelection Method | Sort Method | SortAscending Method | SortDescending Method | SplitTable Method | StartOf Method | ToggleCharacterCode Method | TypeBackspace Method | TypeParagraph Method | TypeText Method | WholeStory Method

Parent Objects | Application Object | Global Object | Pane Object | Window Object

Child Objects | Bookmarks Object | Borders Object | Cells Object | Characters Object | Columns Object | Comments Object | Document Object | Editors Object | EndnoteOptions Object | Endnotes Object | Fields Object | Find Object | Font Object | FootnoteOptions Object | Footnotes Object | FormFields Object | Frames Object | HeaderFooter Object | HTMLDivisions Object | Hyperlinks Object | InlineShapes Object | PageSetup Object | ParagraphFormat Object | Paragraphs Object | Range Object | Rows Object | Sections Object | Sentences Object | Shading Object | ShapeRange Object | SmartTags Object | Tables Object | Words Object | XMLNode Object | XMLNodes Object

See Also | Range Object | Working with the Selection Object