Working with 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 work on a document in Word, you usually select text and then perform an action, such as formatting the text or typing text. In Visual Basic, it is usually not necessary to select text before modifying the text. Instead, you create a Range object that refers to a specific portion of the document. For information on defining Range objects, see Working with Range objects. However, when you want your code to respond to or change the selection, you can do so with the Selection object.

The Select method activates an object. For example, the following instruction selects the first word in the active document.

ActiveDocument.Words(1).Select

For more Select method examples, see the Select method or Selecting text in a document.

The Selection property returns a Selection object that represents the active selection in a document window pane. There can only be one Selection object per document window pane and only one Selection object can be active. For example, the following example changes the paragraph formatting of the paragraphs in the selection.

Selection.Paragraphs.LeftIndent = InchesToPoints(0.5)

For example, the following example inserts the word "Hello" after the selection.

Selection.InsertAfter Text:="Hello"

The following example applies bold formatting to the selected text.

Selection.Font.Bold = True

The macro recorder will often create a macro that uses the Selection property. The following example was created using the macro recorder. The macro applies bold formatting to the first two words in the document.

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

The following example accomplishes the same task without using the Selection property.

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