Re: INDEX of BOOKMARKS

Tech-Archive recommends: Fix windows errors by optimizing your registry




Many thanks Jay.
I will give this a go next week when I am back in the office.
Have a great weekend.
Ciao
DeanH


"Jay Freedman" wrote:

Hi Dean,

It's only a small change. The arguments of the ActiveDocument.Fields.Add
command are fairly similar to those of the ActiveDocument.Hyperlinks.Add
command.

Sub BookmarkIndex()
Dim bk As Bookmark

With Selection
.TypeParagraph
.Style = ActiveDocument.Styles("Index Heading")
.TypeText "Bookmarks" & vbCr
.Style = ActiveDocument.Styles("Index 1")

For Each bk In ActiveDocument.Range.Bookmarks
ActiveDocument.Fields.Add _
Range:=.Range, _
Type:=wdFieldRef, _
Text:=bk.Name & " \h", _
PreserveFormatting:=False
.TypeParagraph
Next
End With
End Sub

--
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.

DeanH wrote:
Jay, I have been using this macro for sometime now and have one
change but cannot get find the right commands.
When the macro is run, the list of bookmarks is displayed perfectly
but their coding is for a hyperlink (as per line
"ActiveDocument.Hyperlinks.Add _") how can this be changed to show
the cross-reference "Ref" instead.
For example the code for a bookmark looks like this:
{ HYPERLINK "DocName.doc" \l "bk001" }
but would like instead:
{ REF bk001 \h }
Many thanks again.
DeanH


"DeanH" wrote:

Fantastic, Jay.
Does exactly as required and will save me so much time.
All the best
DeanH


"Jay Freedman" wrote:

Ah, OK.

As MVP Greg Maxey pointed out to me in an email, this is one
situation where it's easier to use the Selection instead of a Range
object. That way you don't have to keep collapsing after every
insertion. I think this is what you're looking for:

Sub BookmarkIndex()
Dim bk As Bookmark

With Selection
.TypeParagraph
.Style = ActiveDocument.Styles("Index Heading")
.TypeText "Bookmarks" & vbCr
.Style = ActiveDocument.Styles("Index 1")

For Each bk In ActiveDocument.Range.Bookmarks
ActiveDocument.Hyperlinks.Add _
Anchor:=.Range, _
Address:=ActiveDocument.Name, _
SubAddress:=bk.Name, _
TextToDisplay:=bk.Range.Text
.TypeParagraph
Next
End With
End Sub


DeanH wrote:
Jay, thanks for text replacement.
Sorry about the "ned" - podgy-finger-syndrome this morning.
To clarify: my second request is not for an Index so I don't want
the macro to jump to the END of the document, but to display where
the cursor happens to be.
As I want a list of the bookmarks, I wont need the page numbers and
leaders, etc.
But I would like the list still to be hyperlinked.
Sorry for the confusion.
Many thanks again.
DeanH

"Jay Freedman" wrote:

To show the text of the bookmark, replace the line

rg.Text = bk.Name & vbTab

with

rg.Text = bk.Range.Text & vbTab

I don't understand your request to "not jump to the ned [I guess
you meant 'end'] of the document but still be hyperlinked". Can
you clarify?


On Fri, 10 Jul 2009 00:07:01 -0700, DeanH
<DeanH@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hello Jay.
A nice little helpful macro, thanks.
I have a slightly different requirement and have tried to edit
this macro without success.
I am after this Index to show the Bookmark Text (as in Cross
reference, Insert reference to:) not the Bookmark Name, possible?
Also, if this possible, how would the macro be to produce a list
of these Bookmark Names without the page numbers, leaders, etc.
and not jump to the ned of the document but still be hyperlinked?
Many thanks for your assistance.
DeanH

"Jay Freedman" wrote:

Jay Freedman wrote:
NJK wrote:
It will be useful to be able to create an index of all the
bookmark names with their page number and hyperlink to that
bookmark.
eg

[BookmarkName]....[pageNumber]

The use of this will be very helpfull when software
requirement specifiers can have a list of all the bookmarks
from another document instead of having both documents open.
This will cut down in a lot of mouse moving.

An index of bookmarks can be created with a reasonably simple
macro (see http://www.gmayor.com/installing_macro.htm if
needed): [snip]

Sorry, the previous macro produced a plain-text index without
any hyperlinks (which is all you would get from an ordinary
index). This version hyperlinks the page numbers back to the
bookmarks:

Sub BookmarkIndex()
Dim bk As Bookmark
Dim rg As Range

Set rg = ActiveDocument.Range
With rg
.Collapse wdCollapseEnd
.InsertBefore vbCr
.Style = ActiveDocument.Styles("Index Heading")
.Text = "Bookmarks" & vbCr
.Collapse wdCollapseEnd
.Style = ActiveDocument.Styles("Index 1")
.ParagraphFormat.TabStops.Add _
Position:=InchesToPoints(6), _
Alignment:=wdAlignTabRight, _
Leader:=wdTabLeaderDots
End With

For Each bk In ActiveDocument.Range.Bookmarks
rg.Text = bk.Name & vbTab
rg.Collapse wdCollapseEnd

ActiveDocument.Hyperlinks.Add _
Anchor:=rg, _
Address:=ActiveDocument.Name, _
SubAddress:=bk.Name, _
TextToDisplay:=bk.Range.Information( _
wdActiveEndAdjustedPageNumber)
Set rg = ActiveDocument.Range
rg.Collapse wdCollapseEnd
rg.InsertBefore vbCr
rg.Collapse wdCollapseEnd
Next
End Sub


--
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.



.



Relevant Pages

  • Re: Hyperlink / button to bookmark
    ... Word will let me insert a hyperlink into a header but when I click out ... How would I go about '...a toolbar with a button, using a macro'? ... any bookmark when double-clicked. ...
    (microsoft.public.word.numbering)
  • Re: INDEX of BOOKMARKS
    ... I have been using this macro for sometime now and have one change but ... their coding is for a hyperlink (as per line "ActiveDocument.Hyperlinks.Add ... For example the code for a bookmark looks like this: ... "Jay Freedman" wrote: ...
    (microsoft.public.word.docmanagement)
  • Re: Hyperlink field code
    ... Use the #Name syntax in the Hyperlink Dialog - not when directly editing the ... maybe I still haven't understood the syntax correctly. ... "Jay Freedman" wrote: ... Files), click the Bookmark button, and select the range from the ...
    (microsoft.public.word.docmanagement)
  • Re: Word Macro and hyperlinks
    ... FWIW, when I record a macro to hyperlink to an existing bookmark, the recorded ... This reply is posted in the Newsgroup; please post any follow question or reply ...
    (microsoft.public.office.developer.vba)
  • Re: Word Macro and hyperlinks
    ... >> macro works when I run it again. ... The macro runs, and inserts the hyperlink, but if I ... Have you checked at this point whether that bookmark still exists ... > text references - ie see 2.4.2.1.5.3. ...
    (microsoft.public.office.developer.vba)