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, _
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.
This example expands the selection to include the entire sentence.
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, _
Dim objRange As Range
Set objRange = ActiveDocument.Range
objRange.End = 0
objRange.StartOf wdUnits.wdStory, _
Set objRange = ActiveDocument.Range(0,0)
Expanding a range to include the whole document
objSelection.MoveStart wdUnits.wdCharacter, 3
If objSelection.Active = True Then
'this selection is currently active, ie highlighted
There a several ways you can change a given Range object
This redefines the starting and end position of existing selection or range
All characters are counted including non printable characters and hidden characters.
This line of code will set the insertion point to the beginning of the active document
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 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, _
objRange.EndOf Unit:=wdUnits.wdLine, _
When wdMove is used both ends of the range are moved to the end, ie the range is collapsed to a point.
Returns a range representing the resulting insertion point
Extends to the next complete unit
Dim objRange As Range
Set objRange = Application.Selection.Range
Shrinks the range to the next smallest complete unit of text, in the following order (word, sentence, paragraph, section, entire document)
© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext