处理 Selection 对象Working with the Selection Object

在 Word 中处理文档时, 通常会选择文本, 然后执行操作, 例如设置文本格式或键入文本。When you work on a document in Word, you usually select text and then perform an action, such as formatting the text or typing text. 在 Visual Basic 中, 通常不需要在修改文本之前选择文本。In Visual Basic, it is usually not necessary to select text before modifying the text. 有关定义 Range 对象的信息,请参阅处理 Range 对象Instead, you create a Range object that refers to a specific portion of the document. 有关定义Range对象的信息, 请参阅处理 range 对象For information about defining Range objects, see Working with Range objects. 但是, 当您希望代码响应或更改所选内容时, 可以使用**selection** 对象来执行此操作。However, when you want your code to respond to or change a selection, you can do so by using the Selection object.

如果尚未选择文本, 请使用select方法选择与特定对象相关联的文本并创建Selection对象。If text is not already selected, use the Select method to select the text that is associated with a specific object and create a Selection object. 例如,下面的指令选定活动文档中的第一个单词。For example, the following instruction selects the first word in the active document.

Sub SelectFirstWord() 
 ActiveDocument.Words(1).Select 
End Sub

有关详细信息, 请参阅在文档中选择文本For more information, see Selecting text in a document. 如果已选择文本, 则使用**selection** 属性返回一个selection对象, 该对象表示文档中的当前选定内容。If text is already selected, use the Selection property to return a Selection object that represents the current selection in a document. 每个文档只能有一个 Selection 对象,该对象始终访问当前选定内容。There can be only one Selection object per document, and it always accesses the current selection. 下面的示例更改当前选定内容中段落的格式。The following example changes the formatting of the paragraphs in the current selection.

Sub FormatSelection() 
 Selection.Paragraphs.LeftIndent = InchesToPoints(0.5) 
End Sub

本示例在当前选定内容后插入单词"Hello"。This example inserts the word "Hello" after the current selection.

Sub InsertTextAfterSelection() 
 Selection.InsertAfter Text:="Hello " 
End Sub

本示例对选定文本应用加粗格式。This example applies bold formatting to the selected text.

Sub BoldSelectedText() 
 Selection.Font.Bold = True 
End Sub

宏录制器经常会创建使用 Selection 对象的宏。The macro recorder often creates a macro that uses the Selection object. 下面的示例是使用宏录制器创建的。The following example was created using the macro recorder. 该宏选择活动文档中的前两个单词并对其应用加粗格式。This macro selects the first two words in the active document and applies bold formatting to them.

Sub Macro() 
 Selection.HomeKey Unit:=wdStory 
 Selection.MoveRight Unit:=wdWord, Count:=2, Extend:=wdExtend 
 Selection.Font.Bold = wdToggle 
End Sub

下面的示例完成相同的任务,但不选择文本,也不使用 Selection 对象。The following example accomplishes the same task without selecting the text or using the Selection object.

Sub WorkingWithRanges() 
 ActiveDocument.Range(Start:=0, _ 
 End:=ActiveDocument.Words(2).End).Bold = True 
End Sub

支持和反馈Support and feedback

有关于 Office VBA 或本文档的疑问或反馈?Have questions or feedback about Office VBA or this documentation? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.