Collapse confusion

M

mirin

Hi,

1. I need to move the insertion point to the end of the section where
the current insertion point is.

I use the below code:
intSecNum = Selection.Information(wdActiveEndSectionNumber)
ActiveDocument.Sections(intSecNum).Range.Collapse wdCollapseEnd

But the insertion point remains where it is.
I am not getting something right here. Can you please advise?

2. Also, the help says:
Collapse Method: Collapses a range or selection to the starting or
ending position. After a range or selection is collapsed, the starting
and ending points are equal.

What does the phrase "the starting and ending points are equal" mean?
Does it mean that the collapsed range or selection should disappear
(which anyhow doesn't seem to be the case) leaving a single insertion
point in it's place?

On another note, for me the word "collapse" is more closer in meaning
to "implode" so I don't understand quite understand whether the usage
here could be intuitive enough for newbies like me.

Many Thanks,
M
 
H

Helmut Weber

Hi M,

hmm...

It has never ocurred to me that one can collapse a section,
and I wonder, what that would be good for. In fact, I think,
Word should not allow that.

Whatever you do to a range, it usually doesn't affect the selection.
A range is a continuous area in a document, defined by a starting
point and an end point.

Collapsing a range to the start,
means to set the end equal to the start.

Collapsing a range to the end,
means to set the start equal to the end.

With ActiveDocument.Sections(2).Range
.Collapse wdCollapseEnd ' no good, just for learning
.Select
MsgBox .Start & ": " & .End
End With

better:

ActiveDocument.Sections(2).Range.Select
Selection.Collapse Direction:=wdCollapseEnd

But watch out, the selection is now in section #3.

Try:

Sub Testx()
ActiveDocument.Sections(2).Range.Select
Selection.Collapse Direction:=wdCollapseEnd
MsgBox Selection.Information(wdActiveEndSectionNumber)
Selection.MoveLeft
MsgBox Selection.Information(wdActiveEndSectionNumber)
End Sub

It ain't that easy.

Good luck.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jay Freedman

In addition to Helmut's excellent examples, I think a bit of learning
about theory is necessary....

The help topics don't always make it clear what they're talking about,
but there are two possible meanings for the word 'Range' in those
discussions.

- An object within a document may have a .Range property, for example
ActiveDocument.Sections(1).Range. Think of this as the 'size' of that
object, like the width of your house. It won't change unless you edit
the text in the section. You can't collapse it. I would think that
trying to run the .Collapse method of this kind of range should
generate an error, but VBA wasn't written to do that.

- A user-declared Range object is something else. Think of it like a
tape measure that you could stretch out to measure the width of your
house -- it isn't actually attached to the house, and you can roll it
up or move it around. You _can_ do this:

Dim myRange As Range
Set myRange = ActiveDocument.Sections(1).Range
myRange.Collapse Direction:=wdCollapseEnd

The Set statement is like measuring the section, making the Start of
myRange equal the Start of the section's range and making the End of
myRange equal the End of the section's range. Then the Collapse
changes the Start of myRange to be the same as the End of myRange. You
haven't changed the section's range at all; you just "rolled up the
tape measure" to the end of the section (actually, as Helmut pointed
out, the start of the next section if there is one). Then you can use
myRange.Select to move the Selection there.

The Selection behaves like a user-declared Range object, except that
the actual selected area in the document moves with it. That's why you
can do Selection.Collapse.
 
G

Greg Maxey

So in view of the excellent examples and lessons provided by Helmut
and Jay then to get the flashing cursor at the end of the current
section, I would use code something like this:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = Selection.Sections(1).Range
oRng.Start = oRng.End - 2
oRng.Select
Selection.Collapse wdCollapseStart
End Sub
 
T

Tony Jollans

If I may, I'll add my two penn'orth here as you can actually collapse a
Section Range and it does have an effect. It's just that the effect is often
hidden by later actions.

A Range *Object* - any range - is like Jay's tape measure. Every time you
set a range object, you unroll your measure. For a user-defined Range this
is when you use a Set statement. For a system-defined Range object it is
when you use the Range *Property* on the parent object.

This effect is more oftem seen - and taken advantage of, perhaps
unwittingly, - when using Find and Replace, but ...

You *can* do this to achieve the original aim:

With ActiveDocument.Sections(Selection.Sections(1).Index).Range
.Collapse wdCollapseEnd
.Select
End With

But you can *not* use this to do it:

ActiveDocument.Sections(Selection.Sections(1).Index).Range.Collapse
wdCollapseEnd
ActiveDocument.Sections(Selection.Sections(1).Index).Range.Select

... because using the Range Property to get the Range Object in the second
statement rolls out the tape measure anew.
 
M

mirin

Helmut, Jay, Greg, Tony
Wow! Thank you so much for taking out the time to explain things so
clearly.

I now understand how to use the Collapse method correctly and the
different ways in which I can achieve my original aim of moving the
insertion point to the end of a section.

Now I'll have to sit down and meditate on what the Gurus have
taught :)

Thank You!!
M
 

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