Converting endnotes to hyperlink for export to WordPress

The following macro will process both footnotes and endnotes, generating hyperlinks for each, including for footnote/endnote cross-references. It’s best to run the macro only after you’ve finished editing. Note that the original footnote and endnote references aren’t deleted – they’re simply converted to hidden text.

Sub HyperlinkEndNotesFootNotes()
Dim SBar As Boolean           ' Status Bar flag
Dim TrkStatus As Boolean      ' Track Changes flag
Dim Rng1 As Range, Rng2 As Range, i As Long
' Store current Status Bar status, then switch on
SBar = Application.DisplayStatusBar
Application.DisplayStatusBar = True
' Store current Track Changes status, then switch off
With ActiveDocument
  TrkStatus = .TrackRevisions
  .TrackRevisions = False
End With
' Turn Off Screen Updating
Application.ScreenUpdating = False
With ActiveDocument
  'Process all endnotes
  For i = 1 To .Endnotes.Count
    'Give the OS a chance to do any background processing
    DoEvents
    'Update the statusbar
    StatusBar = "Processing Endnote " & i
    'Define two ranges: one to the endnote reference the other to the endnote content
    Set Rng1 = .Endnotes(i).Reference
    Set Rng2 = .Endnotes(i).Range.Paragraphs.First.Range
    With Rng1
      'Format the endnote reference as hidden text
      .Font.Hidden = True
      'Insert a number before the endnote reference and bookmark it
      .Collapse wdCollapseStart
      .Text = i
      .Style = "Endnote Reference"
      .Bookmarks.Add Name:="_ERef" & i, Range:=Rng1
    End With
    'Insert a number before the endnote content and bookmark it
    With Rng2
      'Format the endnote reference as hidden text
      With .Words.First
        If .Characters.Last Like "[ " & vbTab & "]" Then .End = .End - 1
        .Font.Hidden = True
      End With
      'Insert a number before the endnote reference and bookmark it
      .Collapse wdCollapseStart
      .Text = i
      .Style = " Endnote Reference"
      .Bookmarks.Add Name:="_ENum" & i, Range:=Rng2
    End With
    'Insert hyperlinks between the endnote references
    .Hyperlinks.Add Anchor:=Rng1, SubAddress:="_ENum" & i
    .Hyperlinks.Add Anchor:=Rng2, SubAddress:="_ERef" & i
    'Restore the Rng1 endnote reference bookmark
    .Bookmarks.Add Name:="_ERef" & i, Range:=Rng1
  Next 
  'Process all footnotes
  For i = 1 To .Footnotes.Count
    'Give the OS a chance to do any background processing
    DoEvents
    'Update the statusbar
    StatusBar = "Processing Footnote " & i
    'Define two ranges: one to the footnote reference the other to the footnote content
    Set Rng1 = .Footnotes(i).Reference
    Set Rng2 = .Footnotes(i).Range.Paragraphs.First.Range
    With Rng1
      'Format the footnote reference as hidden text
      .Font.Hidden = True
      'Insert a number before the footnote reference and bookmark it
      .Collapse wdCollapseStart
      .Text = i
      .Style = "Footnote Reference"
      .Bookmarks.Add Name:="_FRef" & i, Range:=Rng1
    End With
    'Insert a number before the footnote content and bookmark it
    With Rng2
      'Format the footnote reference as hidden text
      With .Words.First
        If .Characters.Last Like "[ " & vbTab & "]" Then .End = .End - 1
        .Font.Hidden = True
      End With
      'Insert a number before the footnote reference and bookmark it
      .Collapse wdCollapseStart
      .Text = i
      .Style = " Footnote Reference"
      .Bookmarks.Add Name:="_FNum" & i, Range:=Rng2
    End With
    'Insert hyperlinks between the footnote references
    .Hyperlinks.Add Anchor:=Rng1, SubAddress:="_FNum" & i
    .Hyperlinks.Add Anchor:=Rng2, SubAddress:="_FRef" & i
    'Restore the Rng1 footnote reference bookmark
    .Bookmarks.Add Name:="_FRef" & i, Range:=Rng1
  Next 
  'Update the statusbar
  StatusBar = "Finished Processing " & .Endnotes.Count & " Endnotes" & .Footnotes.Count & " Footnotes"
End With
Call HLnkNoteRefs
Set Rng1 = Nothing: Set Rng2 = Nothing
' Restore original Status Bar status
Application.DisplayStatusBar = SBar
' Restore original Track Changes status
ActiveDocument.TrackRevisions = TrkStatus
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub

Sub HLnkNoteRefs()
Dim Fld As Field, StrTgt As String, Rng As Range, StrRef As String
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
For Each Fld In ActiveDocument.Fields
  With Fld
    If .Type = wdFieldNoteRef Then
      StrTgt = ActiveDocument.Bookmarks(Split(Trim(.Code), " ")(1)).Range.Characters.First.Hyperlinks(1).SubAddress
      StrRef = .Result
      Set Rng = .Code
      With Rng
        While .Fields.Count = 0
          .Start = .Start - 1
        Wend
        .Collapse wdCollapseStart
        .Hyperlinks.Add Anchor:=Rng, SubAddress:=StrTgt, TextToDisplay:=StrRef
        .End = .End + 1
        .Hyperlinks(1).Range.Font.Superscript = True
      End With
      .Result.Font.Hidden = True
    End If
  End With
Next
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
End Sub

Quick edit: Crashes with notes that are within bulleted lists. Quick work around is to unbullet –> run macro –> rebullet, but there’s probably a more robust solution.

Leave a Comment