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