What does collapse mean?

E

Erwin Ahlers

Hi,
I've read something about collapsing a range, e.g. the selection. I tried it
but nothing visible happened. As far as I've understood I should not see the
whole range anymore, just the beginning and the end.

Thanks
Erwin
 
D

Dave Lett

Hi Erwin,

Try the following:
Create a new document with two words: One Two
Run the following code:
Dim oRng As Range
With ActiveDocument
Set oRng = .Range(Start:=.Words(1).Start, End:=.Words(1).End)
End With
oRng.Select

Look at your document. Now, run the following code:

Dim oRng As Range
With ActiveDocument
Set oRng = .Range(Start:=.Words(1).Start, End:=.Words(1).End)
End With
With oRng
.Collapse Direction:=wdCollapseEnd
.Select
End With

HTH,
Dave
 
S

Stefan Blom

Range objects are similar to selections in the sense that they are
limited by start and end positions in text. In fact, you can define
range objects by specifying their starting points and endpoints. See
example code below! And the same way you can collapse a selection to
be as "thin" as the insertion point, you can collapse a range to its
starting point or endpoint.

The difference is that unlike selections, you can't see ranges. But
you can see what a range objects refers to if you select it.

For example, the following code inserts some text at the beginning of
the current document and sets a range object to include the first four
characters of that text. At this stage, the Start property of the
range is 0 and the End property is 4. The macro then selects the
range. When the range is collapsed to the end its Start and End
properties are both equal to 4. If selected, you'll see that the range
is "as thin as an insertion point".

Sub TestingRangeObjects()
Dim r As Range
ActiveDocument.Content.InsertBefore "This is some text."
Set r = ActiveDocument.Range(Start:=0, End:=4)
Debug.Print r.Start
Debug.Print r.End
'Start and End equals 0 and 4, respectively.
'Select entire range:
r.Select
r.Collapse wdCollapseEnd
'Print the Start and End properties of the range:
Debug.Print r.Start
Debug.Print r.End
'You'll find that both of these are 4.
'Select the collapsed range:
r.Select
End Sub

If you collapsed the range object using wdCollapseStart instead, you
would find that the values for Start and End would be 0. And if you
used the Select method, the insertion point would end up to the left
of the range instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top