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

When you use the Word user interface to work with a document, you typically select (highlight) text and then do something to the text, such as formatting it, typing new text, or moving it to another location. The Selection object represents the currently selected text in a Word document. The Selection object is always present in a document; if no text is selected, it represents the insertion point. Unlike the Range object, there can only be one Selection object at a time. You can use the Selection object's Type property to get information about the state of the current selection. For example, if there is no current selection, the Selection object's Type property returns wdSelectionIP. The Type property will return one of nine different values represented by the wdSelectionType enumerated constants.

You access a Selection object by using the Selection property. This property is available from the Application, Window, and Pane objects. However, because the Selection property is global, you can refer to it without referencing another object first. For example, the following sample code illustrates how you use the Selection property to get information about the currently selected text:

Sub SelectionCurrentInfo()
   Dim strMessage As String

   With Selection
      If .Characters.Count > 1 Then
         strMessage = "The Selection object in '" & ActiveDocument.Name _
            & "' contains " & .Characters.Count & " characters, " _
            & .Words.Count & " words, " & .Sentences.Count _
            & " sentences, and " & .Paragraphs.Count _
            & " paragraphs."
         MsgBox strMessage
      End If
   End With
End Sub

See Also

Working with Microsoft Word Objects | Working with Document Content | The Range Object | The Selection Object vs. the Range Object | Working with Bookmarks | The Find and Replacement Objects