Selection 对象 (Word)Selection object (Word)

表示窗口或窗格中的当前选内容。Represents the current selection in a window or pane. 选定内容表示文档中的选定(或突出显示)区域,或者代表插入点(如果未选择文档中的任何内容)。A selection represents either a selected (or highlighted) area in the document, or it represents the insertion point if nothing in the document is selected. 每个文档窗格只能有一个 Selection 对象,并且在整个应用程序中只能有一个活动的 Selection 对象。There can be only one Selection object per document window pane, and only one Selection object in the entire application can be active.


此属性已更改。This property has changed. 在带批注的用户选择内容上使用诸如 Selection.BoldRun 命令等 VBA 选择命令时,将不再对用户选择的文本应用加粗格式。Using VBA Selection commands like Selection.BoldRun command on user selection with Comments no longer applies bold formatting on user-selected text.


可以使用 Selection 属性返回 Selection 对象。Use the Selection property to return the Selection object. 如果 Selection 属性未搭配使用任何对象限定符,则 Microsoft Word 将返回活动文档窗口的活动窗格中的选定内容。If no object qualifier is used with the Selection property, Microsoft Word returns the selection from the active pane of the active document window. 以下示例将复制活动文档中的当前选定内容。The following example copies the current selection from the active document.


以下示例删除 Documents 集合中第三个文档的所选内容。The following example deletes the selection from the third document in the Documents collection. 不必激活文档也可以访问其当前选定内容。The document does not have to be active to access its current selection.


以下示例复制活动文档第一个窗格中的所选内容,并将其粘贴到第二个窗格中。The following example copies the selection from the first pane of the active document and pastes it into the second pane.


文本”属性为 Selection 对象的默认属性。The Text property is the default property of the Selection object. 使用此属性可设置或返回当前选定区域中的文本。Use this property to set or return the text in the current selection. 以下示例会将当前选定区域中的文本分配至变量 strTemp,在最后一个字符为段落标记时将其删除。The following example assigns the text in the current selection to the variable strTemp, removing the last character if it is a paragraph mark.

Dim strTemp as String 
strTemp = Selection.Text 
If Right(strTemp, 1) = vbCr Then _ 
 strTemp = Left(strTemp, Len(strTemp) - 1)

Selection 对象有多种方法和属性,可用于折叠、扩展或以其他方式更改当前所选内容。The Selection object has various methods and properties with which you can collapse, expand, or otherwise change the current selection. 以下示例会将插入点移至文档的末尾,并选择最后三行。The following example moves the insertion point to the end of the document and selects the last three lines.

Selection.EndOf Unit:=wdStory, Extend:=wdMove 
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend 
Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend

Selection 对象有多种方法和属性,可用于编辑文档中的所选文字。The Selection object has various methods and properties with which you can edit selected text in a document. 以下示例将选择活动文档中的第一个句子,并将其替换为新段落。The following example selects the first sentence in the active document and replaces it with a new paragraph.

Options.ReplaceSelection = True 
Selection.TypeText "Material below is confidential." 

以下示例删除 Documents 集合中第一个文档的最后一段,并将其粘贴到第二个文档的开头。The following example deletes the last paragraph of the first document in the Documents collection and pastes it at the beginning of the second document.

With Documents(1) 
End With 
With Documents(2).ActiveWindow.Selection 
 .StartOf Unit:=wdStory, Extend:=wdMove 
End With

Selection 对象有多种方法和属性,可用于更改当前所选内容的格式。The Selection object has various methods and properties with which you can change the formatting of the current selection. 以下示例会将当前选定内容的字体从 Times New Roman 更改为 Tahoma。The following example changes the font of the current selection from Times New Roman to Tahoma.

If Selection.Font.Name = "Times New Roman" Then _ 
 Selection.Font.Name = "Tahoma"

可以使用 FlagsInformationType 等属性返回有关当前所选内容的信息。Use properties like Flags, Information, and Type to return information about the current selection. 你可以在某个过程中使用以下示例来确定活动文档中是否选择了内容,如果未选择,则跳过该过程的其余部分。You can use the following example in a procedure to determine whether there is anything selected in the active document; if there is not, the rest of the procedure is skipped.

If Selection.Type = wdSelectionIP Then 
 MsgBox Prompt:="You have not selected any text! Exiting procedure..." 
 Exit Sub 
End If

即便已将选定内容折叠至插入点,它也不必为空。Even when a selection is collapsed to an insertion point, it is not necessarily empty. 例如,Text 属性仍将字符返回到插入点右侧,此字符也会出现在 Selection 对象的 Characters 集合中。For example, the Text property will still return the character to the right of the insertion point; this character also appears in the Characters collection of the Selection object. 但是,已折叠选定内容中的 CutCopy 等调用方法会导致出错。However, calling methods like Cut or Copy from a collapsed selection causes an error.

用户可以选择不代表持续文本的文档中的某个区域(例如,结合使用 Alt 键和鼠标)。It is possible for the user to select a region in a document that does not represent contiguous text (for example, when using the Alt key with the mouse). 由于此类选择行为无可预测,你可能想要将步骤包括在代码中,用于在其上面执行任何操作之前检查选定内容的 Type 属性 (Selection.Type = wdSelectionBlock)。Because the behavior of such a selection can be unpredictable, you may want to include a step in your code that checks the Type property of a selection before performing any operations on it (Selection.Type = wdSelectionBlock).

同样地,包含表单元格的选定内容也会导致出现不可预测的行为。Similarly, selections that include table cells can also lead to unpredictable behavior. Information 属性将会告诉你选定内容是否在表格内 (Selection.Information(wdWithinTable) = True)。The Information property will tell you if a selection is inside a table (Selection.Information(wdWithinTable) = True). 以下示例将确定选定内容是否正常(例如,它不在表格的行或列中,它不是文本的垂直块);可以使用它来先测试当前选定内容,然后再对其执行任何操作。The following example determines if a selection is normal (for example, it is not a row or column in a table, it is not a vertical block of text); you could use it to test the current selection before performing any operations on it.

If Selection.Type <> wdSelectionNormal Then 
 MsgBox Prompt:="Not a valid selection! Exiting procedure..." 
 Exit Sub 
End If

由于 Range 对象与 Selection 对象的许多方法和属性都相同,因此,如果没有必要对当前所选内容进行实际更改,最好使用 Range 对象来处理文档。Because Range objects share many of the same methods and properties as Selection objects, using Range objects is preferable for manipulating a document when there is not a reason to physically change the current selection. 有关 Selection 对象和 Range 对象的详细信息,请参阅处理 Selection 对象处理 Range 对象For more information about Selection and Range objects, see Working with the Selection object and Working with Range objects.



另请参阅See also

支持和反馈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.