Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

This word count code gives inaccurate counts.

15 views
Skip to first unread message

Unknown

unread,
Jan 29, 2006, 4:59:20 PM1/29/06
to
The correct word count (all words in document) via the Tools, Word
Count menu is 600. I need an accurate word count in my macro and
none of these methods work. What am I doing wrong? Please help.

' 544 words reported in document code:
Set mystat = ActiveDocument.ReadabilityStatistics
Selection.GoTo What:=wdGoToHeading, Which:=wdGoToFirst
MyWordCnt = mystat(1).Value ' word count
MsgBox MyWordCnt

' 674 words reported in document code:
Set MyRange = Selection.Range
MyRange.WholeStory
MyWordCnt = MyRange.Words.Count
MsgBox MyWordCnt

' This code usually counts an extra 2 to 6 words in sentences
For Each aPara In MyRange.Paragraphs
For Each aSent In aPara.Range.Sentences
WordCnt = -1 ' Word counts the period as a word
For Each aWord In aSent.Words
WordCnt = WordCnt + 1 ' word counter in sentence
Next
msgbox WordCnt
Next
Next

Jay Freedman

unread,
Jan 29, 2006, 8:27:43 PM1/29/06
to
Hi Charlie,

Anything involving the Words collection in VBA will count punctuation
marks and paragraph marks as "words", giving you a higher count than
you expect.

The ReadabilityStatistics word count seems to be very unreliable.
Testing various selections with the code below, it returned 0 for
selections of two or three words, and sometimes returned a count 1
greater than the number of words actually selected for larger areas.

The most accurate count is the one from the Tools > Word Count dialog,
and the DlgWordCount function shows how to program that.

Sub test()
Dim msg As String
msg = "from Word Count dialog = " & DlgWordCount
msg = msg & vbCr & "from ReadabilityStats = " & RS
MsgBox msg
End Sub

Private Function DlgWordCount() As Long
Dim dlg As Dialog
Set dlg = Dialogs(wdDialogToolsWordCount)
dlg.Execute
DlgWordCount = dlg.Words
Set dlg = Nothing
End Function

Private Function RS() As Long
Dim oRg As Range
If Selection.Type = wdSelectionIP Then
Set oRg = ActiveDocument.Range
Else
Set oRg = Selection.Range
End If
RS = oRg.ReadabilityStatistics("Words").Value
Set oRg = Nothing
End Function

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

Unknown

unread,
Jan 30, 2006, 11:48:25 AM1/30/06
to
Hi Jay,

Thanks a lot. The DlgWordCount function works great. I am trying to
figure out how to apply it to sentences so I can comment all sentences
that contain more than 30 words.

Charlie from Texas

Jay Freedman

unread,
Jan 30, 2006, 9:49:31 PM1/30/06
to
Hi Charlie,

I think this will do it...

Public Sub CheckLongSentences()
Dim oSent As Range


Dim dlg As Dialog
Set dlg = Dialogs(wdDialogToolsWordCount)

On Error GoTo Bye
Application.ScreenUpdating = False

For Each oSent In ActiveDocument.Sentences
With oSent
If Not (.Information(wdWithInTable)) Then
.Select
dlg.Execute
If dlg.Words > 30 Then
.Comments.Add Range:=oSent, _
Text:=CStr(dlg.Words) & " words"
End If
End If
End With
Next oSent

Bye:
Set dlg = Nothing
Selection.HomeKey Unit:=wdStory
Application.ScreenUpdating = True
End Sub

I ran it against the text of "The Wrong Box" by Robert Louis
Stevenson, a fine old Victorian comedy that's 123 pages long in my
copy. The macro commented 457 sentences -- about half the story -- in
88 seconds.

The wdWithinTable check is to avoid an error that occurs if a comment
range includes a table end-of-row marker.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

Unknown

unread,
Jan 31, 2006, 9:14:45 PM1/31/06
to
Jay,

Thanks. I will work on this tomorrow.

Take care,

Charlie from Texas


On Mon, 30 Jan 2006 21:49:31 -0500, Jay Freedman
<jay.fr...@verizon.net> wrote:

>Hi Charlie,
>

0 new messages