Range Object - Collapsing
Collapse
Collapses a Range or Selection object to the start or end position.
After a range or selection is collapsed, the starting and ending points are equal.
objRange.Collapse Direction:=wdCollapseDirection.wdCollapseStart
Direction - The default value is wdCollapseStart.
If you use wdCollapseEnd to collapse a range that refers to an entire paragraph, the range is located after the ending paragraph mark (the beginning of the next paragraph).
The following moves the Range back one character (using MoveEnd) once the Range is collapsed).
Set objRange = ActiveDocument.Paragraphs(1).Range
objRange.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
objRange.MoveEnd Unit:=wdUnits.wdCharacter, _
Count:=-1
The following sets a Range object to the first paragraph and then moves the range forward three paragraphs.
After this macro is run, the insertion point is positioned at the beginning of the fourth paragraph.
Set objRange = ActiveDocument.Paragraphs(1).Range
objRange.Collapse Direction:=wdCollapseDirection.wdCollapseStart
objRange.Move Unit:=wdUnits.wdParagraph, _
Count:=3
objRange.Select
StartOf
Moves the Range or Selection object to the beginning of the specified unit.
lNoOfChars = objRange.StartOf(Unit:=wdUnits.wdStory, _
Extend:=wdMovementType.wdMove)
Unit - The default is wdWord. The unit can only be one of the following constants:
objRange.StartOf(Unit:=wdUnits.wdCell)
objRange.StartOf(Unit:=wdUnits.wdCharacter)
objRange.StartOf(Unit:=wdUnits.wdColumn)
objRange.StartOf(Unit:=wdUnits.wdParagraph)
objRange.StartOf(Unit:=wdUnits.wdRow)
objRange.StartOf(Unit:=wdUnits.wdSentence)
objRange.StartOf(Unit:=wdUnits.wdSection)
objRange.StartOf(Unit:=wdUnits.wdStory)
objRange.StartOf(Unit:=wdUnits.wdTable)
objRange.StartOf(Unit:=wdUnits.wdWord)
Using any other unit will generate a Bad Paramater error.
You can only use wdLine when using a Selection object.
Extend - The default value is wdMove.
This method returns a negative value if the movement is backward in the document,
If the start of the selection is already at the beginning of the specified unit, then the selection does not move.
The following makes sure that the selection is at the beginning of the line.
objRange.StartOf(Unit:=wdUnits.wdLine, _
Extend:=wdMovementType.wdMove)
EndOf
Moves the Range or Selection object to the end of the specified unit.
lNoOfChars = objRange.EndOf(Unit:=wdUnits.wdStory, _
Extend:=wdMovementType.wdMove)
Unit - The default is wdWord. The unit can only be one of the above constants.
You can only use wdLine when using a Selection object.
Extend - The default value is wdMove.
If the end of the selection is already at the end of the specified unit, then the selection does not move.
The following makes sure that the selection is at the end of the line.
Both ends of the range or selection object are moved to the end of the specified unit.
objRange.EndOf(Unit:=wdUnits.wdLine, _
Extend:=wdMovementType.wdMove)
Selection Object - Collapsing
Even when a selection is collapsed to an insertion point it is not empty.
The Text property of a collapsed Selection object will still return the character immediately to the right of the insertion point.
This character will also appear in the Characters collection of the Selection object.
Important
Calling methods like Cut and Copy from a collapsed Selection object will generate an error.
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext