Re: Very Slow character counting in Word 2003

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Edgar E. Cayce (myfullnamenopunctuation_at_yahoo.com)
Date: 05/24/04


Date: Mon, 24 May 2004 10:53:02 -0700

Hmmm,

I tried method #2, where it constantly uses Characters(1) and deletes
from the beginning of the range each time.

I find that it is not any faster, and whats more, I am getting weird
results - all of the spaces are being seen as carriage returns, and my
count of spaces is now showing the number of sentences, not the number
of spaces... I think I must be doing something wrong here.

I start with a range called CurrentRange, and...

    Dim CharCounter As Long
    CharCounter = CurrentRange.Characters.Count

    Do While CharCounter > 1
        With CurrentRange.Characters(1)
            If .Text = " " Then
                Counter.Spaces = Counter.Spaces + 1
            ElseIf .Text = vbTab Then
                Counter.Tabs = Counter.Tabs + 1
            ElseIf .Text = vbCr Then
                Counter.Returns = Counter.Returns + 1
            Else
                Counter.Chars = Counter.Chars + 1
            End If
            ' tests for italics, fonts, etc omitted.
            CharCounter = CharCounter - 1
            .Delete
        End With
    Loop

even skipping the font and formatting tests, this takes forever.

What am I missing?

Ed
        

On Sun, 23 May 2004 13:28:27 +1000, Word Heretic
<myfullname@tpg.com.au> wrote:

>G'day Edgar E. Cayce <myfullnamenopunctuation@yahoo.com>,
>
>Hokay.
>
>The best way to move along with ranges is NOT to use an iterated
>collection. For example, do not use .Paragraphs(99). Every time you
>access a member of the para, words, char etc collections, it is
>DYNAMICALLY CALCULATED on the spot. This is important for BOTH of the
>below solutions.
>
>Find and Replace kicks the pooper out of this if you are able to use
>such methodology. In a similar way, you collapse the find's range to
>the end, then refind on whats left in the document. Always use the
>smallest scope possible for your ranges.
>
>
>Solution 1
>
>This is the general purpose method that moves a range along
>
>'Declare
>
>Dim Scanner as Range
>Dim DocEnd as Long
>
>
>'Init
>
>Set Scanner=ActiveDocument.Content
>Scanner.Collapse wdCollapseStart
>
>'Dont calc this every damn time, do it once
>
>DocEnd=ActiveDocument.Content.End
>
>
>'Sensible With structures enormously speed code
>
>With Scanner
>
>
> 'Main loop
>
> 'We test for the end
>
> While .Start < DocEnd
>
> .MoveEnd
>
>
> 'Do your tests with Scanner
>
> .Collapse wdCollapseEnd
>
> wend
>end with
>
>
>Set Scanner=Nothing
>
>
>Solution 2
>
>I used a variation on this for my report on typefaces used in a doc.
>It deletes as it goes, so iterating the start of the collection is
>always very quick. It is usually only suitable for char by char
>analyses.
>
>You just always
>
>With .Characters(1)
>
> 'blah blah
>
> .Delete
>
>end with
>
>until the character count is 1 in that StoryRange. Corrupt documents
>can play havoc here but that's not something I am willing to go into
>at length here :-) Hopefully it won't be neccesary in a handful of
>years.
>
>
>
>Steve Hudson - Word Heretic
>Want a hyperlinked index? S/W R&D? See WordHeretic.com
>
>steve from wordheretic.com (Email replies require payment)
>
>
>Edgar E. Cayce reckoned:
>
>>Helmut, Klaus, DA,
>>
>>Thank you so much for your help.
>>
>>I am wondering - I need to count font transitions, whenever the face,
>>color or size of the font changes. Since I don;t know what these
>>fonts are in advance, can you think of a way to use Helmut's Find
>>method to count these transitions?
>>
>>Ed
>>
>>
>>On Fri, 21 May 2004 02:56:05 -0700, "Helmut Weber"
>><elmkqznfwvccbf@mailinator.com> wrote:
>>
>>>Hi Edgar,
>>>in addition to Klaus' advice, you may use
>>>search and replace to get the number of
>>>transitions, too:
>>>Don't forget to reset search options before.
>>>' Dim sTime As Long
>>>' sTime = CLng(Timer)
>>>Dim lItalTrns As Long ' transitions
>>>Dim oRng As Range
>>>Set oRng = ActiveDocument.Range
>>>With oRng.Find
>>> .Text = ""
>>> .Font.Italic = True
>>> While .Execute
>>> lItalTrns = lItalTrns + 1
>>> Wend
>>>End With
>>>' MsgBox CLng(Timer) - sTime
>>>MsgBox lItalTrns * 2 ' !
>>>Whereas an excerpt from your code on counting
>>>transitions needs approximately 1 second for
>>>1 page of my test-doc, the above macro needs
>>>1 second for 100 pages. You might have to add
>>>some lines of code for docs that start or end with
>>>italic characters, depending on accuracy.
>>>Greetings from Bavaria, Germany
>>>Helmut Weber, MVP
>>>"red.sys" & chr(64) & "t-online.de"
>>>Word 2002, Windows 2000



Relevant Pages