表示窗口或窗格中的当前选内容。 选定内容表示文档中的选定(或突出显示)区域,或者代表插入点(如果未选择文档中的任何内容)。 每个文档窗格只能有一个 Selection 对象,并且在整个应用程序中只能有一个活动的 Selection 对象。
重要
此方法已更改。 对带批注的用户选择内容使用 VBA 选择命令(如 Selection.BoldRun),不再对用户所选文本应用粗体格式设置,或者对于带批注的用户选择内容使用 Selection.TypeTxt 命令不再插入文本。
备注
可以使用 Selection 属性返回 Selection 对象。 如果 Selection 属性未搭配使用任何对象限定符,则 Microsoft Word 将返回活动文档窗口的活动窗格中的选定内容。 以下示例将复制活动文档中的当前选定内容。
Selection.Copy
以下示例删除 Documents 集合中第三个文档的所选内容。 不必激活文档也可以访问其当前选定内容。
Documents(3).ActiveWindow.Selection.Cut
以下示例复制活动文档第一个窗格中的所选内容,并将其粘贴到第二个窗格中。
ActiveDocument.ActiveWindow.Panes(1).Selection.Copy 
ActiveDocument.ActiveWindow.Panes(2).Selection.Paste
“文本”属性为 Selection 对象的默认属性。 使用此属性可设置或返回当前选定区域中的文本。 以下示例会将当前选定区域中的文本分配至变量 strTemp,在最后一个字符为段落标记时将其删除。
Dim strTemp as String 
 
strTemp = Selection.Text 
If Right(strTemp, 1) = vbCr Then _ 
 strTemp = Left(strTemp, Len(strTemp) - 1)
Selection 对象有多种方法和属性,可用于折叠、扩展或以其他方式更改当前所选内容。 以下示例会将插入点移至文档的末尾,并选择最后三行。
Selection.EndOf Unit:=wdStory, Extend:=wdMove 
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend 
Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
Selection 对象有多种方法和属性,可用于编辑文档中的所选文字。 以下示例将选择活动文档中的第一个句子,并将其替换为新段落。
Options.ReplaceSelection = True 
ActiveDocument.Sentences(1).Select 
Selection.TypeText "Material below is confidential." 
Selection.TypeParagraph
以下示例删除 Documents 集合中第一个文档的最后一段,并将其粘贴到第二个文档的开头。
With Documents(1) 
 .Paragraphs.Last.Range.Select 
 .ActiveWindow.Selection.Cut 
End With 
 
With Documents(2).ActiveWindow.Selection 
 .StartOf Unit:=wdStory, Extend:=wdMove 
 .Paste 
End With
Selection 对象有多种方法和属性,可用于更改当前所选内容的格式。 以下示例会将当前选定内容的字体从 Times New Roman 更改为 Tahoma。
If Selection.Font.Name = "Times New Roman" Then _ 
 Selection.Font.Name = "Tahoma"
可以使用 Flags、Information 和 Type 等属性返回有关当前所选内容的信息。 在某个过程中使用以下示例来确定活动文档中是否选择了内容,如果未选择,则跳过该过程的其余部分。
If Selection.Type = wdSelectionIP Then 
 MsgBox Prompt:="You have not selected any text! Exiting procedure..." 
 Exit Sub 
End If
即便已将选定内容折叠至插入点,它也不必为空。 例如,Text 属性仍将字符返回到插入点右侧,此字符也会出现在 Selection 对象的 Characters 集合中。 但是,已折叠选定内容中的 Cut 或 Copy 等调用方法会导致出错。
用户可以选择不代表持续文本的文档中的某个区域(例如,结合使用 Alt 键和鼠标)。 由于此类选择行为无可预测,你可能想要将步骤包括在代码中,用于在其上面执行任何操作之前检查选定内容的 Type 属性 (Selection.Type = wdSelectionBlock)。
同样地,包含表单元格的选定内容也会导致出现不可预测的行为。 
              Information 属性将会告诉你选定内容是否在表格内 (Selection.Information(wdWithinTable) = True)。 以下示例将确定选定内容是否正常(例如,它不在表格的行或列中,它不是文本的垂直块);可以使用它来先测试当前选定内容,然后再对其执行任何操作。
If Selection.Type <> wdSelectionNormal Then 
 MsgBox Prompt:="Not a valid selection! Exiting procedure..." 
 Exit Sub 
End If
由于 Range 对象与 Selection 对象的许多方法和属性都相同,因此,如果没有必要对当前所选内容进行实际更改,最好使用 Range 对象来处理文档。 有关 Selection 对象和 Range 对象的详细信息,请参阅处理 Selection 对象和处理 Range 对象。
方法
- BoldRun
- Calculate
- ClearCharacterAllFormatting
- ClearCharacterDirectFormatting
- ClearCharacterStyle
- ClearFormatting
- ClearParagraphAllFormatting
- ClearParagraphDirectFormatting
- ClearParagraphStyle
- Collapse
- ConvertToTable
- Copy
- CopyAsPicture
- CopyFormat
- CreateAutoTextEntry
- CreateTextbox
- Cut
- 删除
- DetectLanguage
- EndKey
- EndOf
- EscapeKey
- Expand
- ExportAsFixedFormat
- ExportAsFixedFormat2
- ExportAsFixedFormat3
- Extend
- GoTo
- GoToEditableRange
- GoToNext
- GoToPrevious
- HomeKey
- InRange
- InsertAfter
- InsertBefore
- InsertBreak
- InsertCaption
- InsertCells
- InsertColumns
- InsertColumnsRight
- InsertCrossReference
- InsertDateTime
- InsertFile
- InsertFormula
- InsertNewPage
- InsertParagraph
- InsertParagraphAfter
- InsertParagraphBefore
- InsertRows
- InsertRowsAbove
- InsertRowsBelow
- InsertStyleSeparator
- InsertSymbol
- InsertXML
- InStory
- IsEqual
- ItalicRun
- LtrPara
- LtrRun
- Move
- MoveDown
- MoveEnd
- MoveEndUntil
- MoveEndWhile
- MoveLeft
- MoveRight
- MoveStart
- MoveStartUntil
- MoveStartWhile
- MoveUntil
- MoveUp
- MoveWhile
- Next
- NextField
- NextRevision
- NextSubdocument
- Paste
- PasteAndFormat
- PasteAppendTable
- PasteAsNestedTable
- PasteExcelTable
- PasteFormat
- PasteSpecial
- Previous
- PreviousField
- PreviousRevision
- PreviousSubdocument
- ReadingModeGrowFont
- ReadingModeShrinkFont
- RtlPara
- RtlRun
- Select
- SelectCell
- SelectColumn
- SelectCurrentAlignment
- SelectCurrentColor
- SelectCurrentFont
- SelectCurrentIndent
- SelectCurrentSpacing
- SelectCurrentTabs
- SelectRow
- SetRange
- Shrink
- ShrinkDiscontiguousSelection
- Sort
- SortAscending
- SortByHeadings
- SortDescending
- SplitTable
- StartOf
- ToggleCharacterCode
- TypeBackspace
- TypeParagraph
- TypeText
- WholeStory
属性
- Active
- 应用程序
- BookmarkID
- Bookmarks
- Borders
- Cells
- Characters
- ChildShapeRange
- 列
- ColumnSelectMode
- Comments
- Creator
- Document
- Editors
- End
- EndnoteOptions
- Endnotes
- EnhMetaFileBits
- ExtendMode
- Fields
- 查找
- FitTextWidth
- Flags
- Font
- FootnoteOptions
- Footnotes
- FormattedText
- FormFields
- Frames
- HasChildShapeRange
- HeaderFooter
- HTMLDivisions
- Hyperlinks
- Information
- InlineShapes
- IPAtEndOfLine
- IsEndOfRowMark
- LanguageDetected
- LanguageID
- LanguageIDFarEast
- LanguageIDOther
- NoProofing
- OMaths
- Orientation
- PageSetup
- ParagraphFormat
- Paragraphs
- Parent
- PreviousBookmarkID
- Range
- Rows
- Sections
- Sentences
- Shading
- ShapeRange
- Start
- StartIsActive
- StoryLength
- StoryType
- 样式
- Tables
- Text
- TopLevelTables
- 类型
- WordOpenXML
- Words
- XML
另请参阅
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。