What's wrong with this ?

A

alek.nazarian

Hi all
Just , take a look to this code .

sub deleteAllHyperlinks()
Dim HyperlinksCount as Integer
HyperlinksCount = Selection.range.hyperlinks.count

For i=1 to HyperlinksCount
Selection.Range.Hyperlinks(i).Delete
Next i
End Sub

try selecting a range of text that contains hyperlinks and then execute
this sub-routine . the first iteration of the loop will execute
successfully but the second iteration craps down !

The funniest thing that the third or forth iteration executed
successfully

thanks !
 
J

John

Alek,

When you looping through like this and deleting items as you go, you have to
loop backwards otherwise you're changing the number in the index.

Step through the code below (untested):

Sub deleteAllHyperlinks()
Dim HyperlinksCount As Integer
HyperlinksCount = Selection.Range.Hyperlinks.Count
For i = HyperlinksCount To 1 Step -1
Selection.Range.Hyperlinks(i).Delete
Next i
End Sub

Hope that helps

Best regards

John
 
G

Greg

Change to:

Sub deleteAllHyperlinks()
Dim HyperlinksCount As Long
Dim i As Long
HyperlinksCount = Selection.Range.Hyperlinks.Count
For i = HyperlinksCount To 1 Step -1
Selection.Range.Hyperlinks(i).Delete
Next i
End Sub
 
J

Jonathan West

Greg said:
Change to:

Sub deleteAllHyperlinks()
Dim HyperlinksCount As Long
Dim i As Long
HyperlinksCount = Selection.Range.Hyperlinks.Count
For i = HyperlinksCount To 1 Step -1
Selection.Range.Hyperlinks(i).Delete
Next i
End Sub

or even to this, which would be quicker if there are a large number of
hyperlinks to delete

Sub deleteAllHyperlinks()
Dim HyperlinksCount As Long
Dim i As Long
HyperlinksCount = Selection.Range.Hyperlinks.Count
With Selection.Range
For i = 1 to HyperlinksCount
.Hyperlinks(1).Delete
Next i
End With
End Sub


--
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

Jonathan,

This is true and and a lesson relearned. Someday perhaps I will
remember it.

Thanks.
 
K

Klaus Linke

Or to just turn them into regular text: Ctrl+Shift+F9 (UnlinkFields).

:) Klaus
 

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