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