ComputeStatistics not working on a selection

L

Larry

I'm wondering if I can get any further help on this. Martinique tried
bravely to help but we ran into a wall. According to VBA help in Word
97, this code should count the number of words in the range, which in
this case is the selection. My test code below follows the sample
presented in help. Yet, as I discussed with martinique, the resuling
number is only occasionally correct. Most of the time it's much lower
(sometimes higher) than the correct number. I tried it on another
computer, and it wouldn't work there either. Meanwhile, Martinique
tried it over and over, and it was always correct.

Since I'm following the Help sample so closely, I can't see what I'm
doing wrong. Does anyone have any ideas of why this is not working?

Dim r As Range
Set r = Selection.Range
WordCount = r.ComputeStatistics(Statistic:=wdStatisticWords)

MsgBox "Words in selection: " & WordCount, vbOKOnly, "Word
Counter"
 
L

Larry

It's just bland, filler text, and it doesn't matter what the text is.

Here, I just tested the code again on the most meaningless,
formatting-free text with Arial font. I selected some of it.
ToolsWordCount (which is always correct) told me there were 40 words in
the selection. Then I ran the code I showed you, and it returned a
result of 23 words.

This is a test. This is a test. This is a test. This is a test. This
is a test. This is a test. This is a test. This is a test. This is a
test. This is a test. This is a test. This is a test. This is a
test. This is a test. This is a test. This is a test. This is a
test. This is a test. This is a test. This is a test. This is a
test. This is a test. This is a test. This is a test. This is a
test. This is a test. This is a test. This is a test. This is a
test. This is a test. This is a test. This is a test. This is a
test. This is a test. This is a test. This is a test. This is a
test. This is a test. This is a test. This is a test. This is a
test. This is a test. This is a test. This is a test.

Also, I tested the same thing on another computer that also has Word 97,
and the code doesn't work there either. Also, it is often correct on
the first run of the macro. Then, if you keep increasing the size of
the selection that you're testing, at a certain point you'll start
getting incorrect results.

Also, Doug, I have not sent duplicate messages to newsgroups. The
current message is correctly multiposted to three groups.

Larry
 
K

Klaus Linke

Hi Larry,

Can't replicate the problem in Word2000, either.

Though I found a few posts complaining about a buggy
ComputeStatistics(wdStatisticWords) in Word97, so it looks like a bug that
has silently been fixed.

Ibby suggested to use
Dialogs(wdDialogToolsWordCount).Words
instead, but this seems to be buggy in Word97, too (even though the dialog
always *displays* the proper word count).


Perhaps you could try

With Dialogs(wdDialogToolsWordCount)
.Update
.MsgBox .Words
End With


Regards,
Klaus
 
L

Larry

I spoke too soon. :-(

Earlier, I had tried it over and over and it was fine. But then it
started to get wrong results.

I guess I need to use that Update method, but I can't get it to work.
When I do it the way you showed it to me,

With Dialogs(wdDialogToolsWordCount)
.Update
.MsgBox .Words
End With

that just results in an error statement. So I tried:

Dialogs(wdDialogToolsWordCount).Update
MsgBox Dialogs(wdDialogToolsWordCount).Words

But this is giving me wrong results every time.

So now I'm stuck again.

Larry
 
K

Klaus Linke

So I don't know how someone would know about this.

The VBA help has a list of arguments for built-in dialogs:

wdDialogToolsWordCount:
CountFootnotes, Pages, Words, Characters, DBCs, SBCs,
CharactersIncludingSpaces, Paragraphs, Lines

But MS doesn't exactly go out of its way to document the dialogs.

that just results in an error statement. So I tried:

Dialogs(wdDialogToolsWordCount).Update
MsgBox Dialogs(wdDialogToolsWordCount).Words

But this is giving me wrong results every time.

I had a typo in the code: The dot in ".MsgBox" has to go...
But since your code didn't work, mine likely won't either.

Seems to be really hosed in Word97 :-(

Sorry,
Klaus
 
K

Klaus Linke

One thing you might try:

Perhaps the view (Normal versus Layout) makes a difference, or "Tools >
Options > General > Background pagination".

You could also try to put in "ActiveDocument.Repaginate" or even something
stupid looking like "Selection.Range.Select" to force the dialog to update
(respectively "ComputeStatistics" to re-compute).

But I'm really grasping at straws here...

Klaus
 
L

Larry

I misstated. On the timed display of a dialog box, it was customized
dialog boxes I wanted to do that with, so the ability to do this with
built-in dialogs wouldn't help me with that.

Larry
 

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