Expanding


Range.Expand

Expands the Range or Selection object by a particular unit.

objRange.Expand(Unit:=wdUnits.wdCharacter) 

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

objRange.Expand(Unit:=wdUnits.wdCell) 
objRange.Expand(Unit:=wdUnits.wdCharacter)
objRange.Expand(Unit:=wdUnits.wdColumn)
objRange.Expand(Unit:=wdUnits.wdParagraph)
objRange.Expand(Unit:=wdUnits.wdRow)
objRange.Expand(Unit:=wdUnits.wdSentence)
objRange.Expand(Unit:=wdUnits.wdSection)
objRange.Expand(Unit:=wdUnits.wdStory)
objRange.Expand(Unit:=wdUnits.wdTable)
objRange.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.

objRange.Expand(Unit:=wdUnits.wdCharacter) 

The following expands the Range object to include another sentence

objRange.Expand(Unit:=wdUnits.wdSentence) 


Selection.Extend

Turns extend mode on (sets the ExtendMode property to True), or if extend mode is already on, extends the Selection object to the next larger unit of text.
The progression of selected units of text is as follows: word, sentence, paragraph, section, entire document.

objSelection.Extend(Character:="A") 

Character - The character through which the selection is extended. This argument is case sensitive and must evaluate to a String or an error occurs. Also, if the value of this argument is longer than a single character, the command is ignored.


This example collapses the current selection to an insertion point and then selects the current sentence.

With Selection 
' Collapse current selection to insertion point.
    .Collapse
' Turn extend mode on.
    .Extend
' Extend selection to word.
    .Extend
' Extend selection to sentence.
    .Extend
End With

Here is an example that accomplishes the same task without the Extend method.

With Selection 
' Collapse current selection.
    .Collapse
' Expand selection to current sentence.
    .Expand Unit:=wdUnits.wdSentence
End With

This example makes the end of the selection active and extends the selection through the next instance of a capital "R".

With Selection 
    .StartIsActive = False
    .Extend Character:="R"
End With

Selection - ExtendMode

This property is True when the Extend mode is active.
This property can only be set during run time; attempts to set it in Immediate mode are ignored. The Extend arguments of the EndOf and StartOf methods are not affected by this property.


This example moves to the beginning of the paragraph and selects the paragraph plus the next two sentences.

With Selection 
    .MoveLeft Unit:=wdUnits.wdCharacter, Count:=4, Extend:=True
    .MoveRight Unit:=wdUnits.wdSentence, Count:=2
    .ExtendMode = True
    .MoveUp Unit:=wdUnits.wdParagraph
    .MoveDown Unit:=wdUnits.wdParagraph
End With

This example collapses the current selection, turns on Extend mode, and selects the current sentence.

With Selection 
    .Collapse
    .ExtendMode = True
' Select current word.
    .Extend
' Select current sentence.
    .Extend
End With


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 


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