EndOf Method

Microsoft Word Visual Basic

Moves or extends the ending character position of a range or selection to the end of the nearest specified text unit. This method returns a value that indicates the number of character positions the range or selection was moved or extended (movement is forward in the document).

expression.EndOf(Unit, Extend)

expression    Required. An expression that returns one of the objects in the Applies To list.

Unit    Optional Variant. The unit by which to move the ending character position. WdUnits.

Can be one of the following WdUnits constants:

wdCharacter

wdWord

wdSentence

wdParagraph

wdSection

wdStory

wdCell

wdColumn

wdRow

wdTable.

If expression    returns a Selection object, wdLine can also be used. The default value is wdWord.

Extend    Optional Variant.WdMovementType.

Can be either of the following WdMovementType constants:

wdMove

wdExtend

If wdMove, both ends of the range or selection object are moved to the end of the specified unit. If wdExtend is used, the end of the range or selection is extended to the end of the specified unit. The default value is wdMove.

Remarks

If the both the starting and ending positions for the range or selection are already at the end of the specified unit, this method doesn't move or extend the range or selection. For example, if the selection is at the end of a word and the trailing space, the following instruction doesn't change the selection (char equals 0 (zero)).

char = Selection.EndOf(Unit:=wdWord, Extend:=wdMove)
		

Example

This example extends the selection to the end of the paragraph.

charmoved = Selection.EndOf(Unit:=wdParagraph, Extend:=wdExtend)
If charmoved = 0 Then MsgBox "Selection unchanged"
		

This example moves myRange to the end of the first word in the selection (after the trailing space).

Set myRange = Selection.Characters(1)
myRange.EndOf Unit:=wdWord, Extend:=wdMove
		

This example adds a table, selects the first cell in row two, and then extends the selection to the end of the column.

Set myRange = ActiveDocument.Range(0, 0)
Set myTable = ActiveDocument.Tables.Add(Range:=myRange, _
    NumRows:=5, NumColumns:=3)
myTable.Cell(2, 1).Select
Selection.EndOf Unit:=wdColumn, Extend:=wdExtend