VBA - Expanding


Range.Expand

Expands the Range or Selection object by a particular unit.

oRange.Expand(Unit:=wdUnits.wdCharacter) 

Unit - The default is wdWord. The unit can only be one of the following constants:

oRange.Expand(Unit:=wdUnits.wdCell) 
oRange.Expand(Unit:=wdUnits.wdCharacter)
oRange.Expand(Unit:=wdUnits.wdColumn)
oRange.Expand(Unit:=wdUnits.wdParagraph)
oRange.Expand(Unit:=wdUnits.wdRow)
oRange.Expand(Unit:=wdUnits.wdSentence)
oRange.Expand(Unit:=wdUnits.wdSection)
oRange.Expand(Unit:=wdUnits.wdStory)
oRange.Expand(Unit:=wdUnits.wdTable)
oRange.Expand(Unit:=wdUnits.wdWord)

Using any other unit will generate a Bad Paramater error.
You can only use wdLine when using a Selection object.


This method returns a value (Long) that indicates the number of characters added to the Range.

oRange.Expand(Unit:=wdUnits.wdCharacter) 

The following expands the Range object to include another sentence

oRange.Expand(Unit:=wdUnits.wdSentence) 


You can also define the start and end points of a range by using the Start and End properties of a range.
The following example creates a Range object that refers to the third and fourth sentences in the active document.

Set myDoc = ActiveDocument 
Set myRange = myDoc.Range(Start:=myDoc.Sentences(3).Start, _
                          End:=myDoc.Sentences(4).End)


Dim oRange As Range 
Set oRange = ActiveDocument.Range
oRange.Collapse wdCollapseDirection.wdCollapseStart
oRange.End = 0
oRange.StartOf wdUnits.wdStory, _
                 wdMovementType.wdMove

Set oRange = ActiveDocument.Range(0,0)

Expanding a range to include the whole document

oRange.WholeStory 
oRange.Expand wdUnits.wdStory


There a several ways you can change a given Range object

Range.SetRange

This redefines the starting and end position of existing selection or range
All characters are counted including non printable characters and hidden characters.

oRange.SetRange Start:=0 
                  End:=0

This line of code will set the insertion point to the beginning of the active document

ActiveWindow.Selection.SetRange(0,0) 


Range.StartOf

Its is also possible to refines the starting and end position of a range by a specific number of units

oRange.StartOf Unit:=wdUnits.wdLine, _ 
                 Extend:=wdMovementType.wdMove

Extend has the default of wdMove
When wdMove is used both the ends of the range are moved to the beginning , ie the range is collapsed to a point.
Note: The beginning of the range is not moved if the range is already at the beginning of the specified unit.

oRange.StartOf Unit:=wdUnits.wdWord, _ 
                 Extend:=wdMovementType.wdMove

Range.EndOf


oRange.EndOf Unit:=wdUnits.wdLine, _ 
               Extend:=wdMovementType.wdExtend

When wdMove is used both ends of the range are moved to the end, ie the range is collapsed to a point.



HomeKey



EndKey

Returns a range representing the resulting insertion point



Extends to the next complete unit

Dim oRange As Range 
Set oRange = Application.Selection.Range
oRange.Expand Unit:=wdUnits.wdCharacter



© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext