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