Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2006

Tip: Looking for answers? Try searching our database.

What's wrong with this ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
alek.nazarian@gmail.com - 07 Apr 2006 15:40 GMT
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 !
John - 07 Apr 2006 15:54 GMT
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

> Hi all
> Just , take a look to this code .
[quoted text clipped - 16 lines]
>
> thanks !
Greg - 07 Apr 2006 15:54 GMT
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
Jonathan West - 07 Apr 2006 16:01 GMT
> Change to:
>
[quoted text clipped - 6 lines]
> 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

Signature

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 

Greg - 07 Apr 2006 16:17 GMT
Jonathan,

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

Thanks.
Klaus Linke - 07 Apr 2006 16:18 GMT
Or to just turn them into regular text: Ctrl+Shift+F9 (UnlinkFields).

:-)  Klaus

>> Change to:
>>
[quoted text clipped - 20 lines]
> End With
> End Sub
Jonathan West - 07 Apr 2006 16:57 GMT
> Or to just turn them into regular text: Ctrl+Shift+F9 (UnlinkFields).
>
> :-)  Klaus

That's OK if there are no other fields present in the selection.

Signature

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.