getting rid of hyperlinks

S

simonc

I often copy web pages and paste them into Word. I then
want to remove all the hyperlinks, but I haven't found a
way of doing this except selecting each one individually
and choosing remove from the right click menu.

When I tried to record the sequence of operations into a
macro to do this it had no effect.

Is it possible to write a macro that would take out all
the hyperlinks in a single operation?

Grateful for any help.
 
D

DA

You can use the following to remove your hyperlinks.

Private Sub RemoveHyperLinks()
Dim myLnk As Hyperlink

For Each myLnk In ActiveDocument.Hyperlinks
myLnk.Delete
Next myLnk

End Sub
 
S

simonc

Thanks for this routine. I have pasted it into a Word
macro, but when I run the macro it only takes out the
first hyperlink it finds and then stops. I also noticed
that I had to take out the word Private in the first line
otherwise the macro isn't listed when I go to
Tools>Macro>Macros.

What do I need to do to get it to keep looping until it
reaches the end of the document? (I am using Word 2000, in
case that affects the action of the commands.)

Many thanks for your help.
 
J

Jonathan West

Hi Simon,

This macro will do the job much better

Sub RemoveHyperlinks()
Dim n as Long
With ActiveDocument.Hyperlinks
For n = 1 to .Count
.Item(1).Delete
Next n
End With
End Sub
 
S

simonc

Many thanks Jonathan for that macro.
-----Original Message-----
Hi Simon,

This macro will do the job much better

Sub RemoveHyperlinks()
Dim n as Long
With ActiveDocument.Hyperlinks
For n = 1 to .Count
.Item(1).Delete
Next n
End With
End Sub


--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup



.
 
D

DA

Jonathan,

Quick question.
When I initially tried my routine it worked, yet after
reading yours and Simon's follow up, I revisited another
test case and bingo.. it missed various links in a test
doc.

Is this a known quirk in Word.. or why won't it cycle
through each link if you state for each hyperlink in
ActiveDocument.Hyperlinks ?
 
J

Jonathan West

DA said:
Jonathan,

Quick question.
When I initially tried my routine it worked, yet after
reading yours and Simon's follow up, I revisited another
test case and bingo.. it missed various links in a test
doc.

Is this a known quirk in Word.. or why won't it cycle
through each link if you state for each hyperlink in
ActiveDocument.Hyperlinks ?

The reason is that a For Each loop counts upwards. So the first time you go
round the loop, it deletes the first hyperlink. The second time, it deletes
the second hyperlink - of the ones which are remaining! So your macro only
deletes every second hyperlink.

What mine does is check how many hyperlinks there are, and in the loop it
deletes the first hyperlink of those remaining in the document until there
are no more.

If it were necessary to delete only certain links (e.g. only delete links to
other documents) then it would be necessary to take another approach - to
count backwards. To delete all links whose Address property is not blank,
you would need to do this

Sub RemoveExternalHyperlinks()
Dim n as Long
With ActiveDocument.Hyperlinks
For n = .Count To 1 Step -1
If Len(.Item(n).Address) > 0 Then
.Item(n).Delete
End If
Next n
End With
End Sub

Whenever you deal with collections in the Word object model, and you have a
loop which adds or deletes items from the collection, tyou have to be very
careful with the indexing to be sure that you really are manipulating the
item you think you are. Some collections behave the way collections should
(and are documented to) and automatically readjust their indexes when an
itenm is added, some (like the Hyperlinks collection) do not. Because the
varying behaviour is not documented, I would strongly recommend that you do
not rely on it being one way or the other, or even remaining the same way
between versions of Word.
 
B

ben

Jonathon,
wouldn't:

Sub RemoveHyperlinks()

Dim n as Long
n = ActiveDocument.Hyperlinks.Count

For sentinel = 1 to n
ActiveDocument.Hyperlinks.Item(1).Delete
Next n

End Sub


work better? This way the original number of links is used and not the
"re-dimmed" number each time. Sorry if this language aint too pretty,
I am a Java programmer by trade.
Ben
 
J

Jonathan West

ben said:
Jonathon,
wouldn't:

Sub RemoveHyperlinks()

Dim n as Long
n = ActiveDocument.Hyperlinks.Count

For sentinel = 1 to n
ActiveDocument.Hyperlinks.Item(1).Delete
Next n

End Sub


work better? This way the original number of links is used and not the
"re-dimmed" number each time. Sorry if this language aint too pretty,
I am a Java programmer by trade.


Works exactly the same. In VBA, the limits of a For-Next loop are only
calculated on the initial entry to the loop.

BTW your code would give a compile error, as you have next n after For
sentinal..
 
D

DA

Of course (slap to the forehead!)... somehow I knew all
that, but my dull brain was obviously stuck in neutral.

Thanks for turning on the light for me :)
 

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