Text Property

Microsoft Word Visual Basic

Text Property

       

Range or Selection object: Returns or sets the text in the specified range or selection. Read/write String.

Find or Replacement object: Returns or sets the text to find or replace in the specified range or selection. Read/write String.

Remarks

The Text property returns the plain, unformatted text of the selection or range. When you set this property, the text of the range or selection is replaced.

Example

This example displays the text in the selection. If nothing is selected, the character following the insertion point is displayed.

MsgBox Selection.Text

This example replaces the first word in the active document with "Dear."

Set myRange = ActiveDocument.Words(1)
myRange.Text = "Dear "

This example inserts 10 lines of text into a new document.

Documents.Add
For i = 1 To 10
    Selection.Text = "Line" & Str(i) & Chr(13)
    Selection.MoveDown Unit:=wdParagraph, Count:=1
Next i

This example replaces "Hello" with "Goodbye" in the active document.

Set myRange = ActiveDocument.Content
With myRange.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Hello"
    .Replacement.Text = "Goodbye"
    .Execute Replace:=wdReplaceAll
End With