Range Object - Moving


Move (collapse before)

Range objects include non printing characters such as spaces, tab characters and paragraph marks !!


Collapses the Range or Selection object to its start or end position (referring to an insertion point) and then moves the Range the specified number of units.


This method returns a value (Long) that indicates the number of units by which the start position has actually moved.
If it returns zero (0) then the move was unsuccessful.

lNoOfChars = objRange.Move(Unit:=wdUnits.wdParagraph, _ 
                           Count:=1)

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

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

Using any other unit will generate a Bad Paramater error.
You can only use wdLine when using a Selection object.
Count - The number of units to move. The default value is 1.


If Count is positive, the Range object is collapsed to its end position and moved forward.
If Count is negative, the Range object is collapsed to its start position and moved backward.


You can also control the collapse direction by using the Collapse method before using the Move method.
If the range is in the middle of a unit or isn't collapsed, moving it to the beginning or end of the unit counts as moving it one full unit.


MoveStart

Moves the start position of the Range or Selection object a specified number of units.
This method returns a value (Long) that indicates the number of units by which the start position has actually moved.

lNoOfChars = objRange.MoveStart(Unit:=wdUnits.wdCharacter, _ 
                                Count:=1)

Unit - The default is wdCharacter. The unit can only be one of the above constants.
You can only use wdLine when using a Selection object.
Count - The number of characters to move. The default is 1.


If it returns zero (0) then the move was unsuccessful.


The following moves the start position of the Range one character forward (the selection size is reduced by one character).

objRange.MoveStart Unit:=wdUnits.wdCharacter, _ 
                   Count:=1

The following moves the start position of the Range to the beginning of the line.

objRange.MoveStart Unit:=wdUnits.wdLine, _ 
                   Count:=-1

MoveEnd

Moves the end position of the Range or Selection object a specified number of units.
This method returns the number of units by which the start position has moved.

lNoOfChars = objRange.MoveEnd(Unit:=wdUnits.wdCharacter, _ 
                              Count:=1)

Unit - The default is wdCharacter. The unit can only be one of the above constants.
You can only use wdLine when using a Selection object.
Count - The number of units to move. The default is 1.


If it returns zero (0) then the move was unsuccessful.


The following moves the end of the Range one character backward, the range is reduced by one character.

objRange.MoveEnd Unit:=wdUnits.wdCharacter, _ 
                 Count:=-1

The following moves the end of the Range to the end of the line.

objRange.MoveEnd Unit:=wdUnits.wdLine, _ 
                 Count:=1


MoveUntil (collapse before)

Collapses the Range or Selection object to an insertion point and moves it infront of the first specified character.
This method returns a value (Long) that indicates the number of units by which the Range has actually moved.

lNoOfChars = objRange.MoveUntil(Cset:=  , _ 
                                Count:=1

Cset - One or more characters. This argument is case sensitive.
Count - The maximum number of characters to move. Can also be the wdForward or wdBackward constant. The default value is wdForward.


If Count is positive, the Range is moved forward (starting at the end position).
If Count is negative, the Range is moved backward (starting at the start position).


If Count is greater than 0 (zero), this method returns the number of characters moved plus 1.
If Count is less than 0 (zero), this method returns the number of characters moved minus 1.
If none of the characters are found the method returns zero (0) and the Range is not changed.


The following collapses the Range and moves it forward through the next 100 characters in the document until the character "t" is found.

Set objRange = ActiveDocument.Words(1) 
objRange.MoveUntil Cset:="t", _
                   Count:=100

The following collapses the Range and moves it to the end of the active paragraph.

objRange = ActiveDocument.Selection.Range 
lNoOfChars = objRange.MoveUntil(Cset:=Chr$(13), _
                                Count:=wdConstants.wdForward)

MoveWhile (collapse before)

Collapses the Range or Selection object to an insertion point and moves it infront of the characters while any of the specified characters are found.
This method returns a value (Long) that indicates the number of units by which the Range has actually moved.

lNoOfChars = objRange.MoveWhile(Cset:=  , _ 
                                Count:=1

Cset - One or more characters. This argument is case sensitive.
Count - The maximum number of characters to move. Can also be the wdForward or wdBackward constant. The default value is wdForward.


If Count is a positive, the Range is moved forward (starting at the end position).
If Count is negative, the Range is moved backward (starting at the start position).


If Count is greater than 0 (zero), this method returns the number of characters moved plus 1.
If Count is less than 0 (zero), this method returns the number of characters moved minus 1.
If none of the characters are found the method returns zero (0) and the Range is not changed.


The following collapses the Range and moves it over any consecutive tabs.

objRange.MoveWhile Cset:=wdUnits.vbTab, _ 
                   Count:=wdConstants.wdForward

The following collapses the Range and moves it past any of the following characters "a", "t" or "h" (uppercase or lowercase)

Set objRange = ActiveDocument.Characters(1) 
objRange.MoveWhile Cset:="atiATI", _
                   Count:=wdConstants.wdForward


MoveStartUntil

Moves the start position of the Range or Selection object until one of the specified characters is found.
This method returns the number (Long) of characters that the start position has moved.

lNoOfChars = objRange.MoveStartUntil(Cset:=  , _ 
                                     Count:=1

Cset - One or more characters. This argument is case sensitive.
Count - The maximum number of characters to move. Can also be the wdForward or wdBackward constant. If Count is a positive, the Range is moved forward. If Count is negative, the Range is moved backward. The default value is wdForward.


If Count is greater than 0 (zero), this method returns the number of characters moved plus 1.
If Count is less than 0 (zero), this method returns the number of characters moved minus 1.
If none of the characters are found the method returns zero (0) and the Range is not changed.
If the start position is moved forward to a point beyond the end position, the Range is collapsed and both the start and end positions are moved together.


The following moves the start position backwards until a capital "I" is found.
When the movement is backwards, the Range is expanded.

Selection.MoveStartUntil Cset:="I", _ 
                         Count:=wdConstants.wdBackward

The following moves the start position forwards until a "%" characters is found or for a maximum of ten 10.
When the movement is forwards, the Range is reduced.

Selection.MoveStartUntil Cset:="%", _ 
                         Count:=10


MoveEndUntil

Moves the end position of the Range or Selection object until one of the specified characters is found.
This method returns the number (Long) of characters that the end position has moved.

lNoOfChars = objRange.MoveEndUntil(Cset:=  , _ 
                                   Count:=1

Cset - One or more characters. This argument is case sensitive.
Count - The maximum number of characters to move. Can also be the wdForward or wdBackward constant. If Count is a positive, the Range is moved forward. If Count is negative, the Range is moved backward. The default value is wdForward.



If the movement is forwards then then the Range is expanded.



MoveStartWhile

Moves the start position of the Range or Selection object while any of the specified characters are found.
This method returns the number (Long) of characters that the start position has moved.


objRange.MoveStartWhile(Cset:=  , _ 
                        Count:=1)

Cset - One or more characters. This argument is case sensitive.
Count - The maximum number of characters to move. Can also be the wdForward or wdBackward constant. If Count is a positive, the Range is moved forward. If Count is negative, the Range is moved backward. The default value is wdForward.


If none of the characters are found then the Range does not change.



MoveEndWhile

Moves the end position of the Range or Selection object while any of the specified characters are found.
This method returns the number (Long) of characters that the end position has moved.


objRange.MoveEndWhile(Cset:=  , _ 
                      Count:=1)

Cset - One or more characters. This argument is case sensitive.
Count - The maximum number of characters to move. Can also be the wdForward or wdBackward constant. If Count is a positive, the Range is moved forward. If Count is negative, the Range is moved backward. The default value is wdForward.



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