Log in

View Full Version : [SOLVED:] Hyperlinks in every footer or header VBA



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

macropod
10-05-2010, 05:03 AM
Hi madferit,

The code below shows how to loop through all the headers in the Section. Also, you don't need to (and can't) have independent footers on more than three pages in a Section. Accordingly, the code can be greatly simplified:

Sub Urls()
Selection.Bookmarks.Add ("map")
Dim oHead As HeaderFooter
With ActiveDocument
.PageSetup.DifferentFirstPageHeaderFooter = True
For Each oHead In .Sections(1).Headers
With oHead.Range
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Font.Size = 9
.Font.Bold = True
' replace any existing text in header with new text
.Text = "Text in header" & vbCrLf
.Hyperlinks.Add Anchor:=.Characters.Last, Address:="#map", SubAddress:="", TextToDisplay:="back to bookmark map"
End With
Next
End With
End Sub

madferit
10-05-2010, 05:16 AM
Amazing.. thank you very much!

madferit
10-05-2010, 05:47 AM
just a quick follow up if you or someone could point me in the right direction.
When im covnerting this docx/doc to pdf via Office2010 the bookmark link is inactive, any ideas :dunno

I noticed when i toggeld between alt+ f9 that the link is HYPERLINK "map" if I then edit the link manually and just pressing ok it shows HYPERLINK \l "map" and then it works after convertion

fumei
10-05-2010, 10:29 AM
You may want to try:

Address:="", SubAddress:="map",

instead of:

Address:="#map", SubAddress:=""

madferit
10-05-2010, 10:44 AM
You may want to try:

Address:="", SubAddress:="map",

instead of:

Address:="#map", SubAddress:=""

lol that easy,,,: pray2:

Thank you

fumei
10-05-2010, 11:13 AM
You're welcome.