HomeKey 方法 (Word)Selection.HomeKey method (Word)

将所选内容移动或扩展到指定单位的开头。Moves or extends the selection to the beginning of the specified unit. 此方法返回一个整数, 该整数表示所选内容实际移动的字符数, 如果移动不成功, 则返回 0 (零)。此方法对应于 HOME 键的功能。This method returns an integer that indicates the number of characters the selection was actually moved, or it returns 0 (zero) if the move was unsuccessful.This method corresponds to functionality of the HOME key.


expressionexpression. HomeKey( _Unit_ , _Extend_ )

_表达式_一个代表**Selection** 对象的变量。expression A variable that represents a Selection object.


名称Name 必需/可选Required/Optional 数据类型Data type 说明Description
UnitUnit 可选Optional VariantVariant 单位的所选内容移动或扩展。The unit by which the selection is to be moved or extended. 默认值为wdLineThe default value is wdLine.
ExtendExtend 可选Optional VariantVariant 指定移动所选内容的方式。Specifies the way the selection is moved. 可以是WdMovementType常量之一。Can be one of the WdMovementType constants. 如果该参数值为 wdMove ,选择折叠为一个插入点中并移至指定单位的开头。If the value of this argument is wdMove, the selection is collapsed to an insertion point and moved to the beginning of the specified unit. 如果是 wdExtend ,所选内容的开始扩展到指定单位的开头。If it is wdExtend, the beginning of the selection is extended to the beginning of the specified unit. 默认值为 wdMoveThe default value is wdMove.


本示例将所选内容移至当前文字部分的开头。This example moves the selection to the beginning of the current story. 如果所选内容位于文档正文部分,则本示例将所选内容移至文档的开头。If the selection is in the main text story, the selection is moved to the beginning of the document.

Selection.HomeKey Unit:=wdStory, Extend:=wdMove

本示例将选定内容移到当前行首,将移动的字符数赋给变量 pos。This example moves the selection to the beginning of the current line and assigns the number of characters moved to the pos variable.

pos = Selection.HomeKey(Unit:=wdLine, Extend:=wdMove) 
If pos = 0 Then StatusBar = "Selection was not moved"

另请参阅See also

Selection 对象Selection Object

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