Editing text

This topic includes Visual Basic examples related to the tasks identified in the following sections.

Determine whether text is selected Collapse a selection or range Extend a selection or range Redefine a selection or range Change text

For information about, and examples of, other editing tasks, see the following topics:

Returning text from a document Selecting text in a document Inserting text in a document Manipulating a portion of a document

Determine whether text is selected

The Type](../../../api/Word.Selection.Type.md) property of the Selection](../../../api/Word.Selection.md) object returns information about the type of selection. The following example displays a message if the selection is an insertion point.

Sub IsTextSelected() 
 If Selection.Type = wdSelectionIP Then MsgBox "Nothing is selected" 
End Sub

Collapse a selection or range

Use the Collapse method to collapse a Selection object or a Range object to its beginning or ending point. The following example collapses the selection to an insertion point at the beginning of the selection.

Sub CollapseToBeginning() 
 Selection.Collapse Direction:=wdCollapseStart 
End Sub

The following example cancels the range to its ending point (after the first word) and adds new text.

Sub CollapseToEnd() 
 Dim rngWords As Range 
 
 Set rngWords = ActiveDocument.Words(1) 
 With rngWords 
 .Collapse Direction:=wdCollapseEnd 
 .Text = "(This is a test.) " 
 End With 
End Sub

Extend a selection or range

The following example uses the MoveEnd](../../../api/Word.Selection.MoveEnd.md) method of the Selection object to extend the end of the selection to include three additional words. The MoveLeft, MoveRight, MoveUp, and Move Down methods can also be used to extend a Selection object.

Sub ExtendSelection() 
 Selection.MoveEnd Unit:=wdWord, Count:=3 
End Sub

The following example uses the MoveEnd](../../../api/Word.Range.MoveEnd.md) method of the Range object to extend the range to include the first three paragraphs in the active document.

Sub ExtendRange() 
 Dim rngParagraphs As Range 
 
 Set rngParagraphs = ActiveDocument.Paragraphs(1).Range 
 rngParagraphs.MoveEnd Unit:=wdParagraph, Count:=2 
End Sub

Redefine a selection or range

Use the GetRange method to redefine an existing Selection object or Range object. For more information, see Working with the Selection object or Working with Range objects.

Change text

You can change existing text by changing the contents of a range. The following instruction changes the first word in the active document by setting the Text](../../../api/Word.Range.Text.md) property of a Range object to "The ".

Sub ChangeText() 
 ActiveDocument.Words(1).Text = "The " 
End Sub

You can also use the Delete method or the Selection object or the Range object to delete existing text, and then insert new text using the InsertAfter method or the InertBefore method. The following example deletes the first paragraph in the active document and inserts new text.

Sub DeleteText() 
 Dim rngFirstParagraph As Range 
 
 Set rngFirstParagraph = ActiveDocument.Paragraphs(1).Range 
 With rngFirstParagraph 
 .Delete 
 .InsertAfter Text:="New text" 
 .InsertParagraphAfter 
 End With 
End Sub

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.