editing using "range" slower than with "selection"...?!

A

Andrew V

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
 
D

Doug Robbins - Word MVP

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.

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

Howard Kaikow

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

Andrew V

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

Howard Kaikow

Andrew V said:
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.

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
 

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