Microsoft KB Archive/291447

= Word count appears inaccurate when you use the VBA &quot;Words&quot; property =

Article ID: 291447

Article Last Modified on 1/31/2007

-

APPLIES TO


 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition

-



This article was previously published under Q291447





For a Microsoft Word 97 version of this article, see 159791.



For a Microsoft Word 98 Macintosh Edition version of this article, see 183870.



For a Microsoft Word 2000 version of this article, see 212705.



SYMPTOMS
When you use the Microsoft Visual Basic for Applications (VBA) Words property to return a word count in a Microsoft Word document, the number of words returned appears to be inaccurate.



CAUSE
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. For more information, click the following article number to view the article in the Microsoft Knowledge Base:

290140 How to run the sample code for the Office XP programs from Knowledge Base articles

The Words property returns a collection that represents all the words in a range, selection, or document. The collection includes all punctuation and paragraph marks in the selection or the document.

For example, if you attempt to determine the word count in the following three lines of text

This is x number of words.

This is x number of words.

This is x number of words.

by using the Words property as follows Sub GetWords

MsgBox ActiveDocument.Words.Count

End Sub the result is 24 words.



WORKAROUND
To return only the number of words in a document or a range, excluding paragraph marks and punctuation, use the ComputeStatistics method instead of the Words property.

The ComputeStatistics method returns a word count that does not include paragraph marks or punctuation. By using the sample text from the &quot;Cause&quot; section, the following ComputeStatistics method code returns a count of 18 words: Sub GetWords

MsgBox ActiveDocument.Range.ComputeStatistics(wdStatisticWords)

End Sub For more information, click the following article number to view the article in the Microsoft Knowledge Base:

241333 Word count macro available to count words in a selection

For more information about how to get help with Visual Basic for Applications, click the following article number to view the article in the Microsoft Knowledge Base:

226118 List of resources that are available to help you learn Visual Basic for Applications programming

Additional query words: vb vba vbe prb WD2002 WD2003

Keywords: kbdtacode kbmacro kbprb kbusage KB291447

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.