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"

Re: ComputeStatistics not working on a selection by Larry

Larry
Thu Aug 14 12:39:32 CDT 2003



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




"Doug Robbins - Word MVP" <dkr@mvps.org> wrote in message
news:eFtrv#jYDHA.3232@tk2msftngp13.phx.gbl...
> Hi Larry,
>
> It returns the correct result for me. Paste the text from the range
that
> gives the error into a message so that we can see if there is anything
in it
> that might cause a problem.
>
> AND
>
> Please do not spam the newsgroups by posting the same question
separately to
> multiple newsgroups. Rather, if you are not sure which is the most
> appropriate newsgroup, insert the names of those which you think might
be
> appropriate into the header of a single message. When you do that, an
> answer in one newsgroup will automatically appear in all of the
newsgroups
> to which you posted. That will make it easier for you to find
responses and
> can also reduce the possibility of someone developing an answer to a
> question that has already been answered.
>
> Please post any further questions or followup to the newsgroups for
the
> benefit of others who may be interested. Unsolicited questions
forwarded
> directly to me will only be answered on a paid consulting basis.
>
> Hope this helps
> Doug Robbins - Word MVP
> "Larry" <larry328@att.net> wrote in message
> news:umQ1c4iYDHA.652@tk2msftngp13.phx.gbl...
> >
> >
> > 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"
> >
> >
> >
> >
> >
> >
> >
>
>




Re: ComputeStatistics not working on a selection by Klaus

Klaus
Thu Aug 14 13:43:37 CDT 2003

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


Re: ComputeStatistics not working on a selection by Larry

Larry
Thu Aug 14 14:54:08 CDT 2003




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



"Larry" <larry328@att.net> wrote in message
news:#oSSzvpYDHA.1004@TK2MSFTNGP12.phx.gbl...
>
>
>
> Klaus,
>
> It works! It works! I _knew_ there had to be a way of just getting
> just the _word_ reading from the ToolsWordCount dialog, but no one
knew
> it before. Here's what works:
>
> MsgBox Dialogs(wdDialogToolsWordCount).Words
>
> Or in a fancier msg box:
>
> MsgBox "Words in Selection " &
> Dialogs(wdDialogToolsWordCount).Words, _
> vbOKOnly, "Word Counter"
>
> However, this not easy to discover, since, if you type
>
> Dialogs(wdDialogToolsWordCount).
>
> you do not get "Words" in the drop down list. So I don't know how
> someone would know about this. But in any case, we've just bypassed
the
> ComputeStatistics statement and gone right for the information
returned
> by ToolsWordCount.
>
> Thanks again for the help,
> Larry
>
>
>
>
>
>
>
>
> The built-in ToolsWordCount is not the problem. That always works
> correctly, whether for the whole document or for a selection:
>
> Dialogs(wdDialogToolsWordCount).Show
>
> The problem is using the ComputeStatistics code to return the number
of
> just the words in a selection, so as not to get the number of
paragraph,
> lines, characters, and so on that come with ToolsWordCount.
>
>
>
>
> "Klaus Linke" <fotosatz_kaufmann@t-online.de> wrote in message
> news:Osb#pUpYDHA.1940@TK2MSFTNGP10.phx.gbl...
> > 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
> >
>
>



Re: ComputeStatistics not working on a selection by Klaus

Klaus
Thu Aug 14 16:22:27 CDT 2003

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


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

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


Re: ComputeStatistics not working on a selection by Klaus

Klaus
Thu Aug 14 16:33:38 CDT 2003

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


timed display of dialogs by Larry

Larry
Fri Aug 15 09:55:48 CDT 2003

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





Re: ComputeStatistics not working on a selection by Larry

Larry
Mon Aug 18 14:03:54 CDT 2003

I just checked out the code on Word 2002 and it works properly there.

Larry