Word index problem: hotlinking page references

R

Richard

I'm trying to "hotlink" the page references in a Word index. After creating
the "definnitive" index, I select and manually convert it to plain text then
run my code below. The problem is that the code randomly changes the page
references, often decrementing them by one . Any help would be greatly
appreciated.

Public Sub HotLinkPageNumbers()
'Option Explicit is present at top of code module
'Purpose: hot-link page references in a Word index manually converted to
"plain text"
Dim W As String, IsNum As Boolean 'BeforeMsg, AfterMsg As String
Dim r As Range
Dim n, i, thisnum As Integer, sr, er As Long
If Selection.Start = Selection.End Then
MsgBox "Select range containing the relevant page numbers you want to
'hot-link', then click this button", vbOKOnly
Exit Sub
End If
Set r = ActiveDocument.Range(Start:=Selection.Start, End:=Selection.End)
'This is the manually selected Word index converted to "plain text"
'Application.ScreenUpdating = False
For n = 1 To r.Words.Count
W = r.Words(n).Text
IsNum = False
For i = 1 To Len(W) 'select "numbers" only (i.e. page references in this
instance)
IsNum = (InStr(1, "0123456789", Mid(W, i, 1)) > 0)
If Not IsNum Then
Exit For
End If
Next i
If IsNum Then
thisnum = Val(W) ''' something's going wrong here, often, but not
always, decremented by 1
sr = r.Words(n).Start
er = r.Words(n).End
ActiveDocument.Range(Start:=sr, End:=er).Select
Selection.InsertCrossReference ReferenceType:=wdRefTypeHeading,
ReferenceKind:=wdPageNumber, ReferenceItem:=W, InsertAsHyperLink:=True
'I've tried both W (String) and thisnum (Integer) as argument passed
to ReferenceItem parameter, to no avail!
'The "page number" randomly changes (often, but not alwyas,
decremented by 1)
' ActiveDocument.Repaginate '"Show all" is off before I run
the macro
End If
Next n
'Application.ScreenUpdating = True
Set r = Nothing
End Sub
 
D

David Horowitz

Richard,
This is an interesting one - never checked into the PAGEREF field code
before.
You have more issues than you realize I think - it's not just off by one.
I'm sorry to have to be the one to tell you this. :)
The InsertCrossReference call with the parameters you're specifying:
Selection.InsertCrossReference ReferenceType:=wdRefTypeHeading,
ReferenceKind:=wdPageNumber, ReferenceItem:=W, InsertAsHyperLink:=True
is supposed to link to ranges in the document that have been set to a
Heading style, anything from Heading 1 through Heading 9. (That's the
ReferenceType parameter - it tells it you want to reference Headings.) The
ReferenceItem parameter is supposed to be the index of the heading
occurrence you want to reference. Basically, if you think of all the parts
of your document that are set in any style Heading 1 through Heading 9 as
one big list, this is supposed to be the index into that list. You can see
this if you go to Insert > Reference > Cross-Reference and select Heading as
Reference Type.
Instead, if I understand your code correctly, you were thinking the
ReferenceItem would be the actual page number. Unfortunately this is not
true. The fact that you've only been off by one and only in some cases seems
to be just by strokes of relative luck.
You can set a bookmark at the top of each page (you can do this
programmatically) and then link to them with:
Selection.InsertCrossReference ReferenceType:=wdRefTypeBookmark,
ReferenceKind:=wdPageNumber, ReferenceItem:=W, InsertAsHyperLink:=True
assuming they're the only bookmarks in your document - if not, you'll need
to find out the bookmark number of each of these "page" bookmarks.
I can't see any other way to do exactly what you've looking for. It's
certainly a reasonable thing to be looking to do - create a hyperlink to a
certain page number - in an easier fashion. Maybe someone else can think of
something I haven't.
Good luck!
 
R

Richard

Thanks David for your enlightening feedback.
As you so rightly pointed out, I didn't take on board the link between
InsertCrossReference and Heading styles.
Since I posted, I've found a solution that in essence consists in
bookmarking all relevant index entry markers ({XE....}) then hyperlinking
the
"plain text" pagenumbers in the "converted index" that way (which is
actually quite satisfying). My only worry with it is that I've read in
several sources that an excessive number of bookmarks can cause Word to
flounder (I'm still using 2002 for dev purposes, but will soon transition to
2003, FWIW: I haven't reached that limit yet, but with upwards of 350 pages
in my finalized document I'm holding my breath : -)
Best
Richard
 

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