Publisher) (TextRange 物件
包含附加至圖案的文字,及操作文字的屬性和方法。
註解
使用TextFrame物件的TextRange屬性可傳回您指定之任何圖案的TextRange物件。 使用的 Text 屬性可傳回 TextRange 物件中的文字字串。
使用 ShapeRange.HasTextFrame 屬性來判斷圖形是否有文字圖文框,並使用 TextFrame.HasText 屬性來判斷文字圖文框是否包含文字。
使用Selection物件的TextRange屬性可傳回目前選取的文字。
使用其中一種方法可傳回 TextRange 物件的文字部分: Characters 、 行 、 段落 或 文字 。
使用其中一種方法可以將字元插入 TextRange 物件: InsertAfter 、 InsertBefore 、 InsertDateTime 、 InsertPageNumber 或 InsertSymbol 。
範例
下列範例會將矩形新增至使用中出版物並設定其所包含的文字。
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
方法
- Characters
- Collapse
- Copy
- Cut
- Delete
- Expand
- InsertAfter
- InsertBarcode
- InsertBefore
- InsertDateTime
- InsertMailMergeField
- InsertPageNumber
- InsertSymbol
- 行
- Move
- MoveEnd
- MoveStart
- Paragraphs
- Paste
- Select
- Words
屬性
- 應用程式
- BoundHeight
- BoundLeft
- BoundTop
- BoundWidth
- ContainingObject
- DropCap
- Duplicate
- End
- Fields
- Find
- Font
- Hyperlinks
- InlineShapes
- LanguageID
- 長度
- LinesCount
- MajorityFont
- MajorityParagraphFormat
- ParagraphFormat
- ParagraphsCount
- Parent
- Script
- Start
- 本文
- Text
- WordsCount
另請參閱
支援和意見反應
有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應。
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應