Selecting Text in a Document

Microsoft Word Visual Basic

Selecting Text in a Document

   

Use the Select method to select an item in a document. The Select method is available from several objects, such as Bookmark, Field, Range, and Table. The following example selects the first table in the active document.

Sub SelectTable()
    ActiveDocument.Tables(1).Select
End Sub

The following example selects the first field in the active document.

Sub SelectField()
    ActiveDocument.Fields(1).Select
End Sub

The following example selects the first four paragraphs in the active document. The Range method is used to create a Range object which refers to the first four paragraphs. The Select method is then applied to the Range object.

Sub SelectRange()
    Dim rngParagraphs As Range
    Set rngParagraphs = ActiveDocument.Range( _
        Start:=ActiveDocument.Paragraphs(1).Range.Start, _
        End:=ActiveDocument.Paragraphs(4).Range.End)
    rngParagraphs.Select
End Sub

For more information, see Working with the Selection object.