This word count code gives inaccurate counts.

C

Charlie Mac

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
 
J

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
C

Charlie Mac

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
 
J

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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