I’m an experienced programmer, but not very familiar with VBA.
I’m trying to convert each hyperlinked text in a document to become an endnote, with the original text left in the document, and the endnote showing the URL, as plain text.
The VBA code at the end of this forum post gets me almost there, but the generated footnote shows the original linked text, rather the URL which is what I need. The problem line is the Rng.Endnotes(1).Range.FormattedText
line:
Sub HLnk2FtNt()
Application.ScreenUpdating = False
Dim h As Long, Rng As Range
With ActiveDocument
For h = .Hyperlinks.Count To 1 Step -1
With .Hyperlinks(h)
Set Rng = .Range
With Rng
.Collapse wdCollapseEnd
.Endnotes.Add Rng
.End = .End + 1
End With
Rng.Endnotes(1).Range.FormattedText = .Range.Hyperlinks(1).Address
.Range.Fields.Unlink
End With
Next
End With
Application.ScreenUpdating = True
End Sub
This is failing with a Type mismatch
error at .Address
.
What should that reference be instead?
Think I’ve more-or-less cracked it – though perhaps there is a better way?
Sub HLnk2FtNt()
Application.ScreenUpdating = False
Dim h As Long, Rng As Range
With ActiveDocument
For h = .Hyperlinks.Count To 1 Step -1
With .Hyperlinks(h)
Set Rng = .Range
With Rng
.Collapse wdCollapseEnd
.Endnotes.Add Rng
.End = .End + 1
End With
Rng.Endnotes(1).Range.FormattedText = .Range.FormattedText
.Range.Text = .TextToDisplay
With Rng.Endnotes(1).Range.Hyperlinks(1)
.TextToDisplay = .Address
End With
End With
Next
End With
Application.ScreenUpdating = True
End Sub
by merging in concepts in this answer.
3