Good day,
I have a macro that at some point has to edit text in a TOC. For some
reason simply using ranges to edit the TOC body is *much* slower than using
Selection. This:
If myTOC.Range.Find.Execute("00000") Then
For Each para In myTOC.Range.Paragraphs
If InStr(1, para.Range.Text, "00000") <> 0 Then
para.Range.ParagraphFormat.TabStops.ClearAll
End If
Next para
End If
is slower by half than this:
myTOC.Range.Select
If Selection.Find.Execute("00000") Then
Selection.ParagraphFormat.TabStops.ClearAll
mypara = Selection.Information(wdFirstCharacterLineNumber)
mypage = Selection.Information(wdActiveEndPageNumber)
mysec = Selection.Information(wdActiveEndSectionNumber)
While Selection.Find.Execute("00000") And
Selection.Information(wdActiveEndSectionNumber) = mysec _
And (Selection.Information(wdActiveEndPageNumber) > mypage _
Or Selection.Information(wdFirstCharacterLineNumber) > mypara)
Selection.ParagraphFormat.TabStops.ClearAll
Wend
End If
I would have thought the first is faster, simpler and altogether preferred,
but any ideas why it takes over twice as long to execute?
Thanks,
Andrew
Doug Robbins - Word MVP - 24 Nov 2006 22:16 GMT
It probably depends upon how many paragraphs in the TOC contain the string
for which you are searching.
In your first method, you are iterating through each paragraph in the TOC,
If there are many paragraphs, but only a couple of instanced of the string
for which you are searching, I think that is your answer.

Signature
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
> Good day,
>
[quoted text clipped - 33 lines]
> Thanks,
> Andrew
Howard Kaikow - 24 Nov 2006 22:25 GMT
You are comparing apples and oranges. You have entirely different algorithms
in each code.
One can compare only if the same algorithms are used, one with the Range
object, the other with the Selection object.
Andrew V - 25 Nov 2006 07:00 GMT
Yes I suppose I am comparing apples and oranges, but my real confusion is why
the apples seemed so orange! I rashly assumed that taking advantage of the
Word Object Model (ranges) instead of "physically" traversing my text
(Selection) would be faster, but I did indeed fail to consider the length of
my TOC (6 pages). I figured that to run through about 300 paragraphs using
the range method would take but a moment.
Thank you both for your feedback.
Howard Kaikow - 26 Nov 2006 00:16 GMT
> Yes I suppose I am comparing apples and oranges, but my real confusion is why
> the apples seemed so orange! I rashly assumed that taking advantage of the
[quoted text clipped - 4 lines]
>
> Thank you both for your feedback.
Traversing a document via theParagraph object will always be much slower
than traversing using the Find method, be it using Range or Selection
objects.
You can ONLY compare Range and Selection if you use the same algorithms for
both.
The Expand method is usually the fastest way to traverse a document.
See http://www.standards.com/index.html?ExampleWB2VBA