Variance in word count

I

Ian B

Hi All

I have an ordinary document where if I look at File - Properties - Stats, my
wordcount is 256. This agrees with Tools -WordCount.
In VB I execute x = ActiveDocument.Words.Count and x is 331.
256 is the correct word count.
Can anyone explain how x becomes 331. It's not counting spaces because I
have 244 of them, and I'm showing hidden text.
Can VBA get the count from "File - Properties - stats" figure?

TIA

Ian B
 
D

Doug Robbins - Word MVP

ActiveDocument.BuiltInDocumentProperties(wdPropertyWords) will return the
same number as shown under the Statistics tab of the File Properties dialog.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Julian

Word count from VBA doesn't do the same as Word count from the Tools menu
(go figure!) - the former also counts e.g. paragraph marks (and some other
stuff I can't remember)... would that account for the discrepancy?
 
J

Julian

Forgot to say set a range object and do wdCount =
rangObj.ComputeStatistics(wdStatisticWords) - that gives the same results as
Tools... Word Count

e.g. wdCount = ActiveDocument.Range.ComputeStatistics(wdStatisticWords)
 
F

fumei via OfficeKB.com

Yes indeed, the Words object does count paragraph marks, and other things.

Say a document with the following text (to keep it simple).

This is some text. <p>

So "This is some text." terminated by a paragraph mark.

File > Properties > Statistics Words = 4
Tools > Word Count = 4

Set a Range object:
Set r = ActiveDocument.Range

Using the SAME range object:

r.Words.Count = 6

r.ComputeStatistics(wdStatisticWords) = 4

The Words count returns 6 because the "words" are:

1. This
2. is
3. some
4. text
5. .
6. paragraph mark

The period is counted as a "word", as is the paragraph mark.

The reason? The tools such as File > Properties >Stats; Tools > Word Count;
and the VBA method ComputeStatistics all use internally defined delimiters.

"Words" does not.

There is a similar divergence regarding paragraphs themselves.

Make a brand new document. Insert a 3 x 3 table . Nothing more. Do NOT put
any text in the table. Make a Range object.

Set r = ActiveDocument.Range

You can also use Set r = ActiveDocument.Tables(1).Range. It does not matter.

r.ComputeStatistics(wdStatisticParagraphs) = 0
File > Properties > Statistics Paragraphs = 0
Tools > Word Count Paragraphs = 0

ActiveDocument.Paragraphs.Count = 13

13? The table is 3 x 3, which is 9 cells. Why 13?

Because:

Row1 = 3 cells PLUS End-Of-Row marker....4 paragraphs
Row2 = 3 cells PLUS End-Of-Row marker....4 paragraphs
Row3 = 3 cells PLUS End-Of-Row marker....4 paragraphs

Plus the (always) document terminating paragraph mark. Total = 13 paragraphs.


Now, put some text - This is some text - in Cell (1,1). Do NOT hit the Enter
key.

r.ComputeStatistics(wdStatisticParagraphs) = 1
File > Properties > Statistics Paragraphs = 1
Tools > Word Count Paragraphs = 1

ActiveDocument.Paragraphs.Count = 13

One should take great care when using any COUNT of words or paragraphs. In
particualr, if tables are involved, using a paragraph count has to be well
considered.
 
F

fumei via OfficeKB.com

Just to make it worse......

This is some text.

Remember that Words.Count returned 6?

Change it to:

This is "some" text.

Words.Count = 8

1. This
2. is
3. "
4. some
5. "
6. text
7. .
8. paragraph mark

Each quotation mark is counted as a separate "word".
 
F

fumei via OfficeKB.com

Sorry, I was not perfectly clear.

This is "some" text.

If you make a Range of that:

r.ComputeStatistics(wdStatisticWords) = 4
r.Words.Count = 8
 

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