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 / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrew V - 24 Nov 2006 20:38 GMT
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

Rate this thread:






 
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.