Expanding & Collapsing


Expanding the current Selection

The easiest way to expand a range is to use either the MoveRight or MoveLeft methods.
The Extend argument can either be wdMove or wdExtend. The default is wdMove.

Selection.MoveRight Unit:=wdUnits.wdCharacter, _ 
                    Count:=1, _
                    Extend:=wdMovementType.wdExtend

This method cannot be used with a Range.


You can also use the Expand method. This method returns the number of characters added to the range.

Selection.Expand (Unit:=wdUnits.wdWord) 

This example expands the selection to include the entire sentence.

Selection.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 objRange As Range 
Set objRange = ActiveDocument.Range
objRange.Collapse wdCollapseDirection.wdCollapseStart
objRange.End = 0
objRange.StartOf wdUnits.wdStory, _
                 wdMovementType.wdMove

Set objRange = ActiveDocument.Range(0,0)

Expanding a range to include the whole document

objRange.WholeStory 
objRange.Expand wdUnits.wdStory


objSelection.MoveStart wdUnits.wdCharacter, 3 

If objSelection.Active = True Then 
'this selection is currently active, ie highlighted
End If

There a several ways you can change a given Range object

SetRange Method

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

objRange.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) 


StartOf method

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

objRange.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.

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

EndOf Method


objRange.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 Method



EndKey Method

Returns a range representing the resulting insertion point



objRange.Collapse 

Extends to the next complete unit

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

Shrinks the range to the next smallest complete unit of text, in the following order (word, sentence, paragraph, section, entire document)

objRange.Shrink 


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