VBA create bookmark but retain selection

E

Enda

Hi,
I would appreciate help with the following.

I wish to use VBA to automatically insert a bookmark at the current
cursor location. A problem arises when the user has currently selected
text.

I would like to insert the bookmark at the start of Selection.range
and then restore the original selection.

Does anyone know how to do this?

Thanks,

Enda
 
J

Jonathan West

Enda said:
Hi,
I would appreciate help with the following.

I wish to use VBA to automatically insert a bookmark at the current
cursor location. A problem arises when the user has currently selected
text.

I would like to insert the bookmark at the start of Selection.range
and then restore the original selection.

Does anyone know how to do this?

It can be done with a single statement, as follows.

ActiveDocument.Bookmarks.Add _
Name:="MyBookmark", _
Range:=ActiveDocument.Bookmarks("\StartOfSel").Range

There are several useful "predefined bookmarks" which can be helpful at
times like these.
 
P

Peter Hewett

Hi Enda

It's surprisingly easy:

Public Sub InsertBMAtStartOfSelection()
Dim rngStart As Word.Range

Set rngStart = Selection.Range
rngStart.Collapse wdCollapseStart
ActiveDocument.Bookmarks.Add "BM1", rngStart
End Sub

This code create a bookmark named "BM1" at the selection (if selection is an Insertion
Point) or start of selection if a range of text is selected.

HTH + Cheers - Peter


(e-mail address removed) (Enda), said:
 
D

Dave Neve

Hi

Couldn't help noticing that Jonathan and Peter did this little job in
different ways and hit on sth I have been thinking about.

What is the difference between 'selection' and 'range' as they both seem to
get the same job done at the end of the day.

Thanks in advance
 
P

Peter Hewett

Hi Dave Neve

There's only one Selection object, it's always whatever is selected in the ActiveDocument.
The selection may be an insertion point or a block or text, a column of text or even non
contiguous sections of text in later version of Word. When you programmatically
move/alter the Selection object you can see it change in the document.

Range objects do the same job as the Selection object but you can have as many of them as
you want. Like the selection object they map part of your document. However, unlike the
Selection object moving a Range object or remapping it to a different area of the document
does not show in the document.

For this reason it's generally preferred and quicker to use a Range object as this does
not cause screen updates when you change its mapping.

For some reason the Selection object supports more properties/methods than a Range object.
That's why when you see people asking about moving through a document line-by-line you'll
see the Selection object used, something like this:

Public Sub ReadDocLineByLine()
Selection.HomeKey wdStory
Do
Selection.MoveEnd (wdLine)
MsgBox "(" & Len(Selection.Text) & ") " & Selection.Text
Loop Until Selection.MoveStart(wdLine, 1) = 0
End Sub

That's because you can't use wdLine with a Range object.

For performance reasons and to avoid flickery screen updates wherever possible I use Range
objects. I prefer the aesthetics as well!

HTH + Cheers - Peter
 
T

Theo van der Ster

Hi Enda,

This should do it. First, put the current selection in a range, then
collapse the selection, insert a bookmark and then select the range
again.

Regards,
Theo

Set temprange = Selection.Range
Selection.Collapse
ActiveDocument.Bookmarks.Add Name:="yourbookmark"
temprange.Select
 

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