Share via


TextRange Object [Publisher 2003 VBA Language Reference]

Multiple objects
TextRange
Multiple objects

Contains the text that's attached to a shape, as well as properties and methods for manipulating the text.

Using the TextRange Object

This topic describes how to:

  • Return the text range in any shape you specify.
  • Return a text range from the selection.
  • Return particular characters, words, lines, sentences, or paragraphs from a text range.
  • Insert text, the date and time, or the page number into a text range.

Return a text range from any shape you specify

Use the TextRange property of the TextFrame object to return a TextRange object for any shape you specify. Use the Text property to return the string of text in the TextRange object. The following example adds a rectangle to the active publication and sets the text it contains.

Sub AddTextToShape()
    With ActiveDocument.Pages(1).Shapes.AddShape(Type:=msoShapeRectangle, _
        Left:=72, Top:=72, Width:=250, Height:=140)
        .TextFrame.TextRange.Text = "Here is some test text"
    End With
End Sub

Because the Text property is the default property of the TextRange object, the following two statements are equivalent.

ActiveDocument.Pages(1).Shapes(1).TextFrame _
    .TextRange.text = "Here is some test text"
ActiveDocument.Pages(1).Shapes(1).TextFrame _
    .TextRange = "Here is some test text"

Use the HasTextFrame property to determine whether a shape has a text frame, and use the HasText property to determine whether the text frame contains text.

Return a text range from the selection

Use the TextRange property of the Selection object to return the currently selected text. The following example copies the selection to the Clipboard.

Sub CopyAndPasteText()
    With ActiveDocument
        .Selection.TextRange.Copy
        .Pages(1).Shapes(1).TextFrame.TextRange.Paste
    End With
End Sub

Return particular characters, words, lines, sentences, or paragraphs from a text range

Use one of the following methods to return a portion of the text of a TextRange object: Characters , Lines , Paragraphs , or Words . The following example formats the second word in the first shape on the first page of the active publication. For this example to work, the specified shape must contain text.

Sub FormatWords()
    With ActiveDocument.Pages(1).Shapes(1).TextFrame _
            .TextRange.Words(2).Font
        .Bold = msoTrue
        .Size = 15
        .Name = "Text Name"
    End With
End Sub

Inserting text, the date and time, or the page number into a text range

Use one of the following methods to insert characters into a TextRange object: InsertAfter , InsertBefore , InsertDateTime , InsertPageNumber , or InsertSymbol . This example inserts a new line with text after any existing text in the first shape on the first page of the active publication.

Sub InsertNewText()
    Dim intCount As Integer
    With ActiveDocument.Pages(1).Shapes(1).TextFrame _
            .TextRange
        For intCount = 1 To 3
            .InsertAfter vbLf & "This is a test."
        Next intCount
    End With
End Sub

Properties | Application Property | BoundHeight Property | BoundLeft Property | BoundTop Property | BoundWidth Property | ContainingObject Property | DropCap Property | Duplicate Property | End Property | Fields Property | Find Property | Font Property | Hyperlinks Property | InlineShapes Property | LanguageID Property | Length Property | MajorityFont Property | MajorityParagraphFormat Property | ParagraphFormat Property | Parent Property | Script Property | Start Property | Story Property | Text Property

Methods | Characters Method | Collapse Method | Copy Method | Cut Method | Delete Method | Expand Method | InsertAfter Method | InsertBefore Method | InsertDateTime Method | InsertMailMergeField Method | InsertPageNumber Method | InsertSymbol Method | Lines Method | Move Method | MoveEnd Method | MoveStart Method | Paragraphs Method | Paste Method | Select Method | Words Method

Parent Objects | Cell Object | Field Object | FindReplace Object | HeaderFooter Object | Hyperlink Object | Selection Object | Shape Object | ShapeRange Collection | Story Object | TextFrame Object | TextRange Object

Child Objects | DropCap Object | Fields Object | FindReplace Object | Font Object | Hyperlinks Object | InlineShapes Object | ParagraphFormat Object | Story Object | TextRange Object