Bookmarks

F

Francis Hookham

(I thought I sent ths yeaterday but cannot find it!)

The sub below works well being run from a button on a toolbar.

It would be nice to cut out the InputBox and instead select the text and let
the macro copy that, remove white space and use that as the BookmarkName.

I have tried a couple of times but am missing something - brains probably!

Thanks

Francis Hookham

Sub SetBookmarks()
BookmarkName = InputBox("Bookmark name?")
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=BookmarkName
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Windows("Club Officers 07-08.doc").Activate
End Sub
 
D

Doug Robbins - Word MVP

Use:

With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=Replace(Selection.Range.Text, " ",
"")
.DefaultSorting = wdSortByName
.ShowHidden = False
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
F

Francis Hookham

Great - thanks Doug.

Any ideas for a refinement to help with the following problem - my main use
for this will be to select a heading and use that as the bookmark. I get a
error message with this macro because it cannot cope with the hiden
paragraph mark at the end of the selection. Shift selecting does not help
because that does not eleiminate the paragraph mark. Ok, I do not have to
use all words in the heading but many are single words.

Anyway of removing the hidden paragraph mark (if there is one!)?

Francis
 
R

Russ

Francis,
Will this work for you?

Dim myRange as Range
Set myRange = Selection.Range
If Selection.Characters.Last = vbCr Then
myRange.End = myRange.End - 1
End If
With ActiveDocument.Bookmarks
.Add Range:=myRange, Name:=Replace(myRange.Text, " ",
"")
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

You could use Name:=Trim(myRange.Text) to get rid of just leading and
trailing spaces.
 
R

Russ

Francis,
Two other observations:
1. You could write a macro to bookmark all headings of a particular style.
2. When you use headings with the styles Heading 1, 2... ,
You can view and edit the document in Outline mode and easily collapse and
expand parts of the document, without the need for creating bookmarks.
Type outline in Word help.
 
R

Russ

Hi again Francis,
A third thing is create a TOC

Definition from Word help:
table of contents
A list of the specific headings in a document, along with the page numbers
they appear on.

When you click on a page number in a TOC, you are taken to that page.
 
F

Francis Hookham

Thanks Russ three times - no time yet to take up your helpful comments - it
is all to do with converting a club bulletin written in Word, with TOC, for
a web page and I am feeling my way on that - the TOC works on Heading 1
which I have cut and pasted back as text and removed the tab and page number
(which would not apply on the webpage). Maybe I going at it the hard way and
so will try positing in the bulletin as it stands and adjusting it there but
I am new enough at it to find HTML a bit daunting.

My main problem is I love the challenge of writing a good macro so I
automatically take that route! At my age I ought to know better.

Many thanks - I'll report back in a few days - if I don't get distracted by
another query 'Running macros' I posted late yesterday and I see Jay
Freedman has answered - what a fantastic resource this is thanks to people
like you and him.

Francis
 
F

Francis Hookham

Back again without success - it's more of a challenge than a need since the
TOC way pretty well does what I want but there are times when I should like
to set up bookmarks This is how I intrepreted the suggestion you made in
this posting but I cannot see why it is not working. All a bit above my
head. Any suggestions please Russ:

Sub SetBookmarks()

Dim myBookmarkRange As Range
Set myBookmarkRange = Selection.Range
If Selection.Characters.Last = vbCr Then
myBookmarkRange.End = myBookmarkRange.End - 1
End If

With ActiveDocument.Bookmarks
.Add Range:=myBookmarkRange, Name:=Replace(myBookmarkRange.Text, " ",
"")
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=Trim(myBookmarkRange.Text)
.DefaultSorting = wdSortByName
.ShowHidden = False
End With

End Sub
 
R

Russ

Francis,
Showing the code is a good idea, but you also neglected to say on which line
you are having an error and what is the error message.
I suspect the problem is that your code is .add ing the same bookmark you
just created or there is punctuation in the selection. In other words, each
time you use the .add method, it is attempting to add a bookmark.
If you a trying to get rid of all the space characters in the selection to
create a bookmark name of the selection,
then trim and replace outside of the .add method
or do both those operations inside **one** .add method.

You can 'nest' functions within each other.

There might be some limit on how long your bookmark name may be. But on the
outer part of the 'nest' you could use the Left() function to create a name
of only so many characters.

Use one .add line:
..Add Range:=myBookmarkRange, Name:=Left(Trim(Replace( _
myBookmarkRange.Text, " ", "_")), 15)

Be aware, selecting anything with punctuation ( other than underscores )
will cause an bookmark name error.


Rather than self-generating a bookmark name, it might be better to pop up an
inputbox to ask for a bookmark name. You could have the default text be part
of the selection, which you could edit to make a legitimate bookmark name.

Dim BookmarkName As String
BookmarkName = InputBox("Bookmark Name Suggestion:" & vbCr _
& "Delete any punctuation!" & vbCr & "_ Underscores are OK", _
"What is the Bookmark Name?", Left(Trim _
(Replace(myBookmarkRange.Text, " ", "_")), 45))

Use one .add line:
..Add Range:=myBookmarkRange, Name:=BookmarkName

Good Luck!
 
F

Francis Hookham

Again Russ - apologies for not getting back on all your help - busy, busy -
I'm going to have to let this one keep for a bit longer - I'm limping with
my workhorse computer in for repair and a holiday coming up - might not get
to it until mid-Aug

Thanks VERY much

Francis
 

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

Similar Threads


Top