madferit
10-05-2010, 02:54 AM
Hello, im trying to create a macro that sets a new hyperlink based on a bookmark called map in header or footer on every page in a word document. I can get plain text into a header but not an hyperlink(only first page).. If anyone could please help me?
Sub Urls()
' ActiveDocument.Repaginate
'MsgBox ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
Selection.Bookmarks.Add ("map")
Dim rng As Range
Dim MyRange As Range
With ActiveDocument
.PageSetup.DifferentFirstPageHeaderFooter = True
With .Sections(1).Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Range.Font.Size = 9
.Range.Font.Bold = True
' replace any existing text in header with new text
.Range.Text = "Texst in header" & vbCrLf
For i = 1 To ActiveDocument.ActiveWindow.Panes(1).Pages.Count
Set MyRange = ActiveDocument.Range(1, i)
Set MyRange = MyRange.GoTo(What:=wdGoToPage, Name:="i")
ActiveDocument.Hyperlinks.Add Anchor:=MyRange, Address:="#map", SubAddress:="", TextToDisplay:="back to bookmark map"
i = i + 1
Set MyRange = MyRange.GoTo(What:=wdGoToPage, Name:="i")
Next i
Set rng = .Range
rng.Collapse wdCollapseEnd
End With
End With
End Sub
Sub Urls()
' ActiveDocument.Repaginate
'MsgBox ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
Selection.Bookmarks.Add ("map")
Dim rng As Range
Dim MyRange As Range
With ActiveDocument
.PageSetup.DifferentFirstPageHeaderFooter = True
With .Sections(1).Headers(wdHeaderFooterPrimary)
.LinkToPrevious = False
.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Range.Font.Size = 9
.Range.Font.Bold = True
' replace any existing text in header with new text
.Range.Text = "Texst in header" & vbCrLf
For i = 1 To ActiveDocument.ActiveWindow.Panes(1).Pages.Count
Set MyRange = ActiveDocument.Range(1, i)
Set MyRange = MyRange.GoTo(What:=wdGoToPage, Name:="i")
ActiveDocument.Hyperlinks.Add Anchor:=MyRange, Address:="#map", SubAddress:="", TextToDisplay:="back to bookmark map"
i = i + 1
Set MyRange = MyRange.GoTo(What:=wdGoToPage, Name:="i")
Next i
Set rng = .Range
rng.Collapse wdCollapseEnd
End With
End With
End Sub