PDA

View Full Version : Hyperlinking Footers?



WizHock
08-01-2008, 11:00 AM
Yes I know its a little, and by that I mean A LOT, like my other post, but I need to be able to hyperlink text in a footer the same way this code hyperlinks a user picked word in a document or numbers in a document:

Public Sub WordsToLinks()
'Gets the total number of pages
Dim lPgs As Long
Dim myrange As Range
Dim lastPage As Range
Set lastPage = ActiveDocument.Range.GoTo(wdGoToPage, wdGoToLast)
Set myrange = ActiveDocument.Range.GoTo(wdGoToPage, wdGoToFirst)
lPgs = 1
Do Until lastPage.Start = myrange.Start
Set myrange = myrange.GoTo(wdGoToPage, wdGoToNext)
lPgs = lPgs + 1
Loop
Set myrange = Nothing
Set lastPage = Nothing

'Header
'Dim rng As Range
'With ActiveDocument.Sections(1)
' With .Headers(wdHeaderFooterPrimary)
' Set rng = .Range.Duplicate
' rng.Collapse wdCollapseEnd
' rng.InsertBefore vbTab & "Page of "
' rng.Collapse wdCollapseStart
' rng.Move wdCharacter, 6
' ActiveDocument.Fields.Add rng, wdFieldPage
' Set rng = .Range.Duplicate
' rng.Collapse wdCollapseEnd
' ActiveDocument.Fields.Add rng, wdFieldNumPages
' End With
' End With


'Defines a counter (i), a string to represent that counter
'in Call LinkWord(string, string)
'Defines the file path and file extention
Dim i As Integer
Dim Str As String
Dim ThePath As String
Dim TheExtention As String

'set values for variables
i = 1
pth = "C:\Documents and Settings\ww\Desktop\AutoLinkTest\Doc"
ext = ".doc"

'Loop through each page number and link
For i = 1 To lPgs Step 1
Str = i
Call LinkWord(Str, pth & Str & ext)
Next i

'Uncomment to have the user define what word they want to hyperlink and what to link to
'Dim TheWord As String
'Dim TheLink As String
'TheWord = InputBox("Please type what word you want to link", "Word to Hyperlink")
'TheLink = InputBox("Please type where you would like to link to", "Hyperlink path")
'Call LinkWord(TheWord, TheLink)
End Sub


Public Sub LinkWord(SearchWord As String, HyperLink As String)

Dim ThisWord As Range ' The Range of the current Word
Dim lWord As Long ' Word counter

lWord = 1

With ActiveDocument

' Loop through all the Words in the document
While lWord < .Words.Count

' Grab the Range of the current Word
Set ThisWord = .Words(lWord)

' If the current Word matches the one we want
' (ignoring case and extra spaces)
If Trim(ThisWord.Text) = SearchWord Then

' Shrink the Range to remove any spaces, otherwise
' the link will be the Word plus the space after it
With ThisWord
.End = .End - Len(.Text) + Len(Trim(.Text))
End With

' Add a hyperlink to it
Hyperlinks.Add _
Anchor:=ThisWord, _
Address:=HyperLink, _
TextToDisplay:=ThisWord.Text

End If

' Step to the next word
lWord = lWord + 1

Wend

End With

Set ThisWord = Nothing


End Sub


I've tried accessing ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary) to get what I want but I lose the .Words.Count properties. I tried using .PageNumbers.Count but then I lose .HyperLinks Add ...

Any suggestions?