Microsoft Word EndOf Method VBA (Visual Basic for Applications) example

Microsoft Word EndOf Method VBA (Visual Basic for Applications) example


Moves or extends the ending character position of a Range or Selection. EndOf method associated with a range or selection object which helps extending the selection or move selection.


Selection.EndOf(unit, extend)

  1. Unit: its an optional parameter, decides the movement of cursor in a range. The unit is derived from wdUnits enum which extends various constants like wdCell, wdCharacter, wdCharacterFormatting and so on.
  2. Extend: its an optional parameter, which can be used to extend the selection using WdMovementType enum having:
    • wdMove : moves cursor
    • wdExtend : makes selection

Code example

Public Sub EndOfExample()
    'Declare selection variable
    Dim oSelection As Selection
    'bind selection
    Set oSelection = Selection
    'Move selection line end
    oSelection.EndOf Unit:=wdLine, Extend:=wdMove
    'Move select rest of paragraph from current selection
    oSelection.EndOf Unit:=wdParagraph, Extend:=wdExtend
    'Memory cleanup
    Set oSelection = Nothing
End Sub

Output before

Output after

Next >> System Object in Microsoft Word a complete reference

Leave a Reply

Your email address will not be published. Required fields are marked *