TextRange 对象 (Publisher)

包含附加到形状上的文本,以及用于操作文本的属性和方法。

备注

使用 TextFrame 对象的 TextRange 属性可返回您指定的任何形状的 TextRange 对象。 使用 Text 属性返回 TextRange 对象中的文本字符串。

使用 ShapeRange.HasTextFrame 属性可确定形状是否具有文本框架,并使用 TextFrame.HasText 属性确定文本框架是否包含文本。

使用 Selection 对象的 TextRange 属性可返回当前选定的文本。

使用下列方法之一来返回 TextRange 对象的文本的某个部分︰ 字符段落单词

使用下列方法之一将字符插入 TextRange 对象︰ 新段落活动InsertDateTimeInsertPageNumberInsertSymbol

示例

下面的示例将一个矩形添加到当前出版物,并设置其包含的文本。

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

Text 属性是 TextRange 对象的默认属性,因为以下两个语句是等效的。

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"

下面的示例将选定内容复制到剪贴板。

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

下面的示例设置当前出版物的第一页上第一个形状中第二个单词。 为本示例生效,指定的形状必须包含文本。

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

本示例在当前出版物的第一页上第一个形状中的现有文本之后插入新的文本行。

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

方法

属性

另请参阅

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。