hyperlinks

M

Mark Fitzgerald

How do you remove ALL hyperlinks in a document at once?...
(not going into each link individually and removing one by
one!)
 
J

Jonathan West

Mark Fitzgerald said:
How do you remove ALL hyperlinks in a document at once?...
(not going into each link individually and removing one by
one!)

By running this macro

Sub Removehyperlinks()
Dim x as Long
For x = 1 to ActiveDocument.Hyperlinks.Count
ActiveDocument.Hyperlinks(1).Delete
Next x
End Sub
 
L

Larry

Do Until ActiveDocument.Hyperlinks.Count = 0
ActiveDocument.Hyperlinks(1).Delete
Loop

Larry
 
L

Larry

Oh, yeah, Jonathan's code is better than the code I showed you, for
reasons Jonathan explained in a post on 10/21:

---- Original Message ----
From: "Jonathan West" <[email protected]>
Newsgroups: microsoft.public.word.vba.general
Sent: Tuesday, October 21, 2003 5:26 p.m.
Subject: Re: Removing all hyperlinks from word doc

For ... each is a general solution to this problem, because it's guaranteed
to iterate every member of the collection, even if the loop itself adds or
removes members:

Dim pHyperlink as Word.Hyperlink
For each pHyperlink in ActiveDocument.Hyperlinks
pHyperlink.delete
Next

Hi Jezebel,

I suggest you try running that code. You will find that it does in fact
only
delete half the links.

Your first answer was better, but there's no need to check the balue of
ActiveDocument.Hyperlinks.Count every time you go round the loop.
Therefore,
your first answer can be speeded up a bit by this means

Dim x As Long
With ActiveDocument.Hyperlinks
For x = 1 to ActiveDocument.Hyperlinks.Count
.Item(1).Delete
Next x
End With

With a small number of hyperlinks, this perhaps doesn't make a
significant
difference, but if you are deleting thousands of links, avoiding that
extra
access of the object model is a noticeable speed improvement.

It might be more expected to use .Item(x).Delete instead of
..Item(1).Delete.
The latter is again a bit faster since it is often quicker to access the
first item in a collection than the last in the Word object model.
 

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