PDA

View Full Version : Remove Select Hyperlinks...



vklind
03-10-2006, 02:13 PM
I have several Word macros that can and do delete all hyperlinks in any Word document. None provide what I am looking for. I need a Word macro that will delete all external hyperlinks leaving the text in place (but not preserving any underlines or coloring)... I also need to leave in place any hyperlinks that are internal (bookmark) hyperlinks. I want the macro to run without human intervention. No 'yes' or 'no' questions. :banghead:

fumei
03-10-2006, 07:20 PM
Dim oHyperlink As Word.Hyperlink
For Each oHyperlink In ActiveDocument.Hyperlinks()
If Left(oHyperlink.Address, 4) = "http" Then
oHyperlink.Delete
End If
Next

mdmackillop
03-11-2006, 02:36 AM
This is trickier than it looks.
Internal (within the same document) hyperlinks don't seem to have an "address" .
Within the same folder, within other folders and on other drives hyperlinks appear to have addresses, but without http.
Web pages have the http. (What about ftp?)
So I tried the following, which fails dismally, and I don't know why!


Sub DelH()
Dim oHyperlink As Word.Hyperlink
i = 0
MsgBox ActiveDocument.Hyperlinks.Count
For Each oHyperlink In ActiveDocument.Hyperlinks()
i = i + 1
Debug.Print ActiveDocument.Hyperlinks(i).Address & " - " & _
ActiveDocument.Hyperlinks(i).TextToDisplay
If oHyperlink.Address <> "" Then
oHyperlink.Delete
End If
Next
MsgBox ActiveDocument.Hyperlinks.Count
End Sub

R_Rajesh
03-11-2006, 07:15 AM
mdmackillop, calling it recursively seems to work. btw I am unable to access the file you attached. Do I need some special rights or something??



Sub RemoveLink()
Dim oHyperlink As Word.Hyperlink
For Each oHyperlink In ActiveDocument.Hyperlinks()
If oHyperlink.Address <> "" Then
oHyperlink.Delete: RemoveLink
End If
Next
End Sub

mdmackillop
03-11-2006, 07:25 AM
Hi Rajesh
Nothing special about the file. Resaved and reposted here. It contains 6 assorted hyperlinks, but the code only deletes 3 of them.

R_Rajesh
03-11-2006, 07:44 AM
Yeh, if we were to delete the 3rd hyperlink, 4th becomes 3rd and 5th becomes 4th and next time around in the loop, it skips one...



Sub DelH()
Dim oHyperlink As Word.Hyperlink
i = 0
MsgBox ActiveDocument.Hyperlinks.Count
For Each oHyperlink In ActiveDocument.Hyperlinks()
oHyperlink.Range.Select: MsgBox "a"
i = i + 1
Debug.Print ActiveDocument.Hyperlinks(i).Address & " - " & _
ActiveDocument.Hyperlinks(i).TextToDisplay
If oHyperlink.Address <> "" Then
oHyperlink.Delete
End If
Next
MsgBox ActiveDocument.Hyperlinks.Count
End Sub

mdmackillop
03-11-2006, 08:07 AM
Copying and pasting in a few more hyperlinks, it appears to be deleting alternative links only. More are deleted by rerunning the code, until eventually they're gone.

TonyJollans
03-11-2006, 06:12 PM
Really, Malcolm! :)

Surely you know to delete collection members from the end backwards.

MWE
03-11-2006, 07:04 PM
Really, Malcolm! :)

Surely you know to delete collection members from the end backwards.
darn :banghead: and here I thought I could contribute to the Word forum with something useful.

mdmackillop
03-12-2006, 01:27 AM
mdmackillop - Public Profile
Biography: Still learning after all these years.


Thanks Tony (and MWE)


Sub DelH()
With ActiveDocument
For i = .Hyperlinks.Count To 1 Step -1
If .Hyperlinks(i).Address <> "" Then .Hyperlinks(i).Delete
Next
End With
End Sub

TonyJollans
03-12-2006, 05:02 AM
:)

vklind
03-13-2006, 06:56 AM
Thanx people... great stuff!

fumei
03-14-2006, 12:23 AM
I not sure I got what happened there. I thought the request WAS to delete external (ie. http) links and leave everything else alone? In which case, the point that internal links do not have an full address (true) is completely irrelevant. Good...then they do not have http, and therefore are not external, and...can be ignored.

External hyperlinks (http) - delete
internal (bookmark) hyperlinks (NOT http) - do not delete

Did I miss something......again?

mdmackillop
03-14-2006, 01:41 AM
Hi Gerry,
I think "internal" is open to interpretation. Internal to the document, or to files on the PC. I went with the latter, yours deals with the former. I don't know which the OP was after or has used, but I think there are two valid solutions here in either case.
Regards
Malcolm

TonyJollans
03-14-2006, 02:42 AM
Hyperlinks come in many shapes and sizes. The Hyperlinks Collection contains all the HYPERLINK Fields (all external hyperlinks, if you like, not just http and other protocols, but also local files) - it does not contain other Fields which have ("internal") hyperlink capability (REF, PAGEREF, etc.). Deleting all hyperlinks in the Hyperlinks Collection fits the bill as far as I can see.

vklind
03-14-2006, 07:28 AM
My apologies for being ambiguous... in fact I was wanting to delete all hyperlinks that are not internal to the document. Some stuff I am working with, but not a lot, have TOCs, indexes and appendixes that are linked. It is these that I wish to retain, but delete all others.

Again, thanx to all...:beerchug: