From: Greg Maxey on
I'm sorry. The following response was intended for another post.

"Greg Maxey" <gmaxey(a)mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote in message
news:O866nmXgKHA.5568(a)TK2MSFTNGP02.phx.gbl...
> Sub FindAndItalicBoundText()
> Dim oRng As Range
> Set oRng = ActiveDocument.Content
> With oRng.Find
> .ClearFormatting
> .Text = "^^<*>^^"
> .Forward = True
> .Wrap = wdFindStop
> .MatchWildcards = True
> Do While .Execute
> With oRng
> .Characters.First.Delete
> .Characters.Last.Delete
> .Font.Italic = True
> .Collapse wdCollapseEnd
> End With
> Loop
> End With
> End Sub
>
>
> "mrgou" <rgoubet(a)gmail.com> wrote in message
> news:90e49421-60ea-436e-aaab-13ad87f53858(a)p8g2000yqb.googlegroups.com...
>> Hi,
>>
>> I want to delete all hyperlinks from my document, but the following
>> macro doesn't work
>>
>> Sub DeleteLinks()
>> Dim MyHLink As Hyperlink
>> For Each MyHLink In ActiveDocument.Hyperlinks
>> MyHLink.Delete
>> Next MyHLink
>> End Sub
>>
>> Any idea why?
>>
>> Thanks!
>>
>> Raph
>
>


From: Pesach Shelnitz on
Hi Raph,

Every object in a collection has an index. When you delete an object, the
indices of the remaining objects change (decrease by 1). Thus, when the code
goes to look for the object with the index 2, it won't look for the object
with the index 1 anymore even though there is such an object. The solution is
to start with the highest index and work backwards.

--
Hope this helps,
Pesach Shelnitz
My Web site: http://makeofficework.com


"mrgou" wrote:

> On 20 déc, 13:28, Pesach Shelnitz <pesach18(AT)hotmail.com> wrote:
> > In general, to delete all the objects from a collection in VBA, you need a
> > For loop that iterates through the collection in descending order. The first
> > of the following macros converts all the hyperlinks in the active document to
> > ordinary text, and the second macro completely erases all the hyperlinks.
> [snip]
> > Hope this helps,
>
> Indeed it does, although I don't understand how your second macro is
> different from mine in the end. I thought a For Each loop in a
> collection would iterate through all objects of that collection. Your
> goes backwards, but why does it make a difference?
>
> Many thanks for your help!
>
> Raph
> .
>
From: mrgou on
On 20 déc, 14:38, "Greg Maxey"
<gma...(a)mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote:
> <I thought a For Each loop in a collection would iterate through all objects
> of that collection ...

> The issue is that you are changing the number of items in the collection as
> the procedure runs.  This is sort of like moving the goal posts after a game
> begins ;-).

Got it now. And I learned something in the process.

Thanks!

Raph
From: mrgou on
On 20 déc, 15:19, Pesach Shelnitz <pesach18(AT)hotmail.com> wrote:
> Hi Raph,
>
> Every object in a collection has an index. When you delete an object, the
> indices of the remaining objects change (decrease by 1). Thus, when the code
> goes to look for the object with the index 2, it won't look for the object
> with the index 1 anymore even though there is such an object. The solution is
> to start with the highest index and work backwards.

Many thanks for the help and explanation!

Raph
From: Jonathan West on

"Pesach Shelnitz" <pesach18(AT)hotmail.com> wrote in message
news:507CC8E1-D818-4EE6-9DFC-528029364DAB(a)microsoft.com...
> Hi Raph,
>
> In general, to delete all the objects from a collection in VBA, you need a
> For loop that iterates through the collection in descending order. The
> first
> of the following macros converts all the hyperlinks in the active document
> to
> ordinary text, and the second macro completely erases all the hyperlinks.
>
> Sub ConvertHyperlinksToText()
> Dim i As Long
>
> With ActiveDocument
> For i = .Hyperlinks.Count To 1 Step -1
> .Hyperlinks(i).Delete
> Next
> End With
> End Sub
>
> Sub DeleteHyperlinks()
> Dim i As Long
>
> With ActiveDocument
> For i = .Hyperlinks.Count To 1 Step -1
> .Hyperlinks(i).Range.Delete
> Next
> End With
> End Sub
>


In fact, if you have a large number of hyperlinks to delete, the following
versions will be somewhat faster. You probably won't notice much difference
unless there are hundreds or even thousands of hyperlinks in a document.

Sub ConvertHyperlinksToText()
Dim i As Long

With ActiveDocument
For i = 1 to .Hyperlinks.Count
.Hyperlinks(1).Delete
Next
End With
End Sub

Sub DeleteHyperlinks()
Dim i As Long

With ActiveDocument
For i = 1 to .Hyperlinks.Count
.Hyperlinks(1).Range.Delete
Next
End With
End Sub


The code looks a bit odd - repeatedly deleting the first link - but it works
because you are repeatedly deleting the first hyperlink in the document from
among those that remain after the last deletion.

The reason it is faster is that accessing the first member of a collection
is almost always faster than accessing the last, and the speed advantage
increases with the number of items in the collection.


--
Regards
Jonathan West