Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2007

Tip: Looking for answers? Try searching our database.

Collapse confusion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mirin - 18 Dec 2007 08:31 GMT
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
Helmut Weber - 18 Dec 2007 12:36 GMT
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
Jay Freedman - 18 Dec 2007 13:22 GMT
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.

>Hi M,
>
[quoted text clipped - 48 lines]
>
>Vista Small Business, Office XP

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
Tony Jollans - 18 Dec 2007 15:19 GMT
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.

Signature

Enjoy,
Tony

> In addition to Helmut's excellent examples, I think a bit of learning
> about theory is necessary....
[quoted text clipped - 91 lines]
> Email cannot be acknowledged; please post all follow-ups to the newsgroup
> so all may benefit.
mirin - 19 Dec 2007 01:59 GMT
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

On Dec 19, 12:19 am, "Tony Jollans" <My forename at my surname dot
com> wrote:
> 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
[quoted text clipped - 123 lines]
> > Email cannot be acknowledged; please post all follow-ups to the newsgroup
> > so all may benefit.
Greg Maxey - 18 Dec 2007 14:52 GMT
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

> Hi,
>
[quoted text clipped - 24 lines]
> Many Thanks,
> M
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.