Removing hyperlinks

J

J Lenihan

I have a number of documents with tons of hypelinks that no longer are valid.
Is there a way to progromatically search thru the document and remove the
links? The text is ok to stay but removing the links and color would be great!

Thanks!
 
G

Greg Maxey

Try:

Sub ScratchMacro()
Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldHyperlink Then
oFld.Unlink
End If
Next

End Sub
 
J

Jonathan West

Greg Maxey said:
Try:

Sub ScratchMacro()
Dim oFld As Field
For Each oFld In ActiveDocument.Fields
If oFld.Type = wdFieldHyperlink Then
oFld.Unlink
End If
Next

End Sub


Sorry Greg, but that only remove half the hyperlinks (try it!)

This will remove all of them

Sub ScratchMacro()
Dim iFld As Long
For iFld = ActiveDocument.Fields to 1 Step -1
If ActiveDocument.Fields(iFld).Type = wdFieldHyperlink Then
ActiveDocument.Fields(iFld).Unlink
End If
Next iFld
End Sub

Better still (i.e. quicker) is to avoid bothering with all the non-hyperlink
fields, like this

Sub ScratchMacro()
Dim iFld As Long
For iFld = ActiveDocument.Hyperlinks to 1 Step -1
ActiveDocument.Hyperlinks(1).Delete
Next iFld
End Sub

(Yes, it is repeatedly deleting the first hyperlink in the document. Think
about why that works <g>)


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Greg Maxey

Jonathan,

I did try it and just tried it again. It works here to unlink 1 of 1 or 10
of 10. It unlinks all in the document.

I remember you showing the technique of working from the last to the first
using step -1 when deleting indexed items, but that doesn't appear to be
necessary in this case. At least the code I posted is working to unlink all
hyperlinks in my test document. I am using made up links like (e-mail address removed)
etc.

..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top