Bookmark won't collapse

D

Dave Neve

HiThe first two lines of the following code work but the 3 line doesn't.At
least I think it doesn't cos nothing happens which is close to 'collapse'
for me.The bookmark is still available and visible in it's entirety.Thanks
in advanceActiveDocument.Bookmarks.Add Range:=Selection.Range, Name:="temp"
ActiveDocument.ActiveWindow.View.ShowBookmarks = True
Selection.Collapse Direction:=wdCollapseStart
 
P

Perry

Yr below code will collapse to the start of a range.

First, select a word in yr document and then run the code.
You will notice that a bookmark is beautifully placed around
the word you selected previously, and the current selection (after code
execution)
is perfectly at the start of the newly created bookmark.

You coded this perfectly, unless the above description does not match yr
intention.
if the latter, y'll have to rephrase what you want to achieve.

Krgrds,
Perry
 
J

Jay Freedman

Hi Dave

You misunderstand what the Collapse method is for.

The Selection (the highlighted portion of the text) might be
"extended" -- that is, it may cover one or more characters, rather
than being "collapsed" to a single point between characters.

Whether the Selection is extended or collapsed, the code inserts a
bookmark there. If the Selection is extended, the bookmark will appear
as a pair of square braces [ ] surrounding the Selection. If the
Selection is collapsed, the braces overlap so their vertical bars are
together, and the result looks like a fat upper-case i.

Then the code tells the Selection to collapse to its starting point.
If the Selection was already collapsed, nothing will happen; if it was
extended, it will now be that single point. In either case, the
bookmark won't be affected at all -- it's only the Selection that
collapses.

If you want to remove the text that's inside the bookmark, do this:

ActiveDocument.Bookmarks("temp").Range.Delete

This will also delete the bookmark (the braces) itself along with the
enclosed text.
 
P

Peter Hewett

Hi Dave Neve

Collapsing the Selection object or a Range object does *not* affect the
underlying text. In the case of the Selection object, if one or more characters
are selected it collapses that selection to an insertion point at the begriming
(Direction:=wdCollapseStart) or end (Direction:=wdCollapseEnd) of the text. Try
selecting a block of text in your document and run just one line of code:
Selection.Collapse Direction:=wdCollapseStart

in the VBA IDEs Immediate window (Ctrl+G) and then look at your document.

Almost the same thing happens with the Range object. But Range objects are not
directly reflected in the document, so when you collapse one you can't directly
see any changes to it. If you want to see what a Range object maps to in your
document try the following snippet of code:

A useful tip for displaying what a Range object maps to in your document is to
select the Range object in the Immediate window. Say you have a Range object
called "rngTest". To display what it maps to, type this into the Immediate
window:
rngTest.Select

HTH + Cheers - Peter
 
D

Dave Neve

Trying to understand VBA Bruce.

But I've got this point now (thanks everyone).

Collapse doesn't mean what I thought it does and in fact I can collapse with
the best of 'em.

Regards

Dave Neve
 

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