How to convert the hyperlinks in a document to plain text
Or: How to prevent URLs from being converted to hyperlinks while I type
Article contributed by Dave Rado, with acknowledgements to Ibby and Jonathan West
If you want to prevent urls being converted to hyperlinks as you type them, select Tools + Autocorrect + Autoformat As You Type, and under “Replace as you type”, turn off “Internet and network paths with hyperlinks”.
If you want to convert all existing hyperlinks in a document into plain text, you could run the following macro:
Sub GetRidOfHlinks()
Dim oHlink As Hyperlink,
i As Long
For i = ActiveDocument.Hyperlinks.Count
To
1 Step -1
ActiveDocument.Hyperlinks(i).Delete
Next i
End Sub
This does not delete the text, it just converts the hyperlink fields to plain text.
The reason that a For Each loop can't be used is that many Word collections, including the Hyperlinks collection, are buggy; if you delete a member of the collection using a For Each loop, Word loses track, and as a result, only every second hyperlink gets deleted if you use For Each. And you have to count from the top to the bottom of the collection, as shown, or Word will lose track.
The above code always works well in Word 97, but unfortunately, in Word 2000, if your Hyperlink character style is defined to have bold formatting, you will end up with manual bold formatting applied to your URLs after running the code (another bug). To get round that, you could use the following variation instead, which always works, in Word 97 and above:
Sub
GetRidOfHlinksWithoutApplyingBold()
Dim oHlink As Hyperlink,
i As Long, MyRange As Range
For i = ActiveDocument.Hyperlinks.Count
To
1 Step -1
With ActiveDocument.Hyperlinks(i)
Set
MyRange = .Range
.Delete
MyRange.Font.Reset
End With
Next i
End Sub
On the other hand, if you want to remove the hyperlink fields but still have the text of the URLs formatted with the Hyperlink character style, you could use the following variation on the same theme:
Sub
GetRidOfHlinksButPreserveCharacterStyle()
Dim oHlink As Hyperlink,
i As Long, MyRange As Range
For i = ActiveDocument.Hyperlinks.Count
To
1 Step -1
With
ActiveDocument.Hyperlinks(i)
Set
MyRange = .Range
.Delete
MyRange.Style = wdStyleHyperlink
End With
Next i
End Sub
Preserving hyperlinks within your Table of Contents
In Word 2000 and above, if your table of contents contains the /h switch (in other words, if the text in the Table of Contents – not only the page numbers – hyperlinks to your headings), the above code will remove those text hyperlinks as well (although they will be automatically regenerated when you next update the table of contents). That is, it won't unlink the Table of Contents, and the page numbers will still hyperlink to the headings, but the text in the TOC won't hyperlink to the Headings after running the above code.
To prevent that from happening, you could use the following code instead:
Sub GetRidOfHlinksExceptInToc()
Dim oHlink As Hyperlink,
i As Long, MyRange As
Range, _
oToc As TableOfContents, LinkIsInToc
As Boolean
For i = ActiveDocument.Hyperlinks.Count
To
1 Step -1
With
ActiveDocument.Hyperlinks(i)
Set
MyRange = .Range
LinkIsInToc =
False
For
Each
oToc In ActiveDocument.TablesOfContents
If
MyRange.InRange(oToc.Range) Then
LinkIsInToc = True
Exit For
End If
Next
oToc
If Not
LinkIsInToc
Then
.Delete
MyRange.Font.Reset
'or use MyRange.Style = wdStyleHyperlink if you prefer
End If
End With
Next i
End Sub
As an aside, if you have no other fields in the document, you can convert all the hyperlinks to plain text manually by Selecting All and pressing Ctrl+Shift+F9 (unlink fields). That's a big “if”, though, and it's a risk I'd rather not take. The above macros do run very fast.