1
votes

I'm new to VBA and managed to put together a simple macro that will convert all text with a certain style to a hyperlink (pasted below), and I would really like it to do the same thing with all footnote text as well, but none of the answers online I've tried have worked so far.

Any thoughts?

Sub FindLinkStyle()
Dim strStyle As String
strStyle = "Subtle Reference"
Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .text = ""
    .ClearFormatting
    .Style = strStyle
        Do While .Execute
        ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
        Address:="#link"
        Selection.Style = ActiveDocument.Styles("Normal")
        Loop
    End With
End Sub
1

1 Answers

2
votes

Unlike the Word UI, "Find" works only on the "Story" you specify. So in your case it will depend on where Selection is. To specifically address Footnotes, see how a Range object is set to the "footnotes story" in the following code sample, then that Range used in a Find.

Sub FindInFootnote()
    Dim rngFT As word.Range

    Set rngFT = ActiveDocument.StoryRanges(wdFootnotesStory)
    With rngFT.Find
        .Text = ""
        .Style = "Subtle Reference"
        .Replacement.Text = "XXXXX!!!!!"
        .Execute Replace:=wdReplaceAll
    End With
End Sub