Cross-reference to a specific page

C

Colin Beaver

I'd like to insert cross-references to specific pages, without adding a
bookmark.
The reason for this is that I am writing a macro which will convert the page
numbers in an index to links to those pages. I know I'll have to rerun this
every time I want to regenerate the index. If anyone knows how to do this it
will save a lot of time and effort.

If it's not possible without bookmarks, then I'd like to use this strategy
in a macro.
1. Find the next page number in the index.
2. Add a cross-reference to a bookmark called PageX where X is the page
number.
3. Go To the page and add the bookmark.

Repeat through the index.

I am working with a 500 page document, so I am reluctant to add a bookmark
on every required page. The problem with my macro below is that you can't add
a jump to an as-yet non-existent bookmark. What are your suggestions?

Sub gotopage()
'Find next number
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<[0-9]*>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
pagenum = Selection
' Construct bookmark name
pagenum = "Page" + pagenum
'Insert reference to book mark
Selection.InsertCrossReference ReferenceType:="Bookmark",
ReferenceKind:= _
wdPageNumber, ReferenceItem:=pagenum, InsertAsHyperlink:=True, _
IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
Selection.Find.ClearFormatting
'Add bookmark if it doens't already exist
If ActiveDocument.Bookmarks.Exists(pagenum) = False Then
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=pagenum
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=pagenum
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
End If
End Sub
 
C

Colin Beaver

Is my request unclear, or does no one know the answer? I'll happily reword it.
Colin
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?Q29saW4gQmVhdmVy?=,
Is my request unclear, or does no one know the answer?
Perhaps a bit of both. One reads to the point where you say
"do it without a bookmark", which is simply not possible
(cross-reference or hyperlinks require bookmarks), and that
sort of turns one off.

Then, your steps are a bit backwards. You can't insert a
cross-reference to a bookmark before the bookmark exists.
But there's nothing stopping you from using RANGES, rather
than the SELECTION object. Then you never "lose your
place". And the code will probably execute more quickly.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 

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