PDA

View Full Version : Solved: Word Count and Character Count



MonteCristo
06-25-2010, 06:27 PM
Hello,

I need some help with VBA in Word 2007. More specifically, I need to know how Word counts "words", as is displayed in the "Word Count dialog box" at the bottom of the page. One thing I know for sure, ActiveDocument.Words.Count returns a different result than the one expected.

As well, how would I search for a character? Say I want to know how many times "." is used in a document, what function can I use?

Finally, if you guys know of a good online tutorial with VBA in Word 2007, please let me know. So far I found nothing of real value...

Thank you.

fumei
06-28-2010, 09:41 AM
Finally, if you guys know of a good online tutorial with VBA in Word 2007, please let me know. So far I found nothing of real value...

Search harder. There is a fair amount out there.


As well, how would I search for a character? Say I want to know how many times "." is used in a document, what function can I use?
Use Find. Demo attached. Click "Period Highlights" on top toolbar.

I need to know how Word counts "words", as is displayed in the "Word Count dialog box" at the bottom of the page. One thing I know for sure, ActiveDocument.Words.Count returns a different result than the one expected. It depends. The menu Word Count uses delimiters - it ignores .,() and other stuff.

VBA Word.Count uses a Range object (as does Word.Count) BUT it does NOT use delimiters. Thus, if you have:

This is a sentence (I think).

And you select it, the menu Tools > Word Count returns 6.

This
is
a
sentence
I
think

However, if you execute:
Sub WordMyCount()
MsgBox Selection.Words.Count
End Sub
it will return either 8 or 9 - depending on whether you included the paragraph mark at the end.

1. This
2. is
3. a
4. sentence
5. (
6. I
7. think
8. ).
9. pargraph mark

Not only that, but suppose you had an accident space between the ending bracket and the period.

This is a sentence (I think)[space].

Then the word count = 10

zaboo9
06-28-2010, 11:17 AM
Montecrist also asked,

As well, how would I search for a character? Say I want to know how many times "." is used in a document, what function can I use?

Finally, if you guys know of a good online tutorial with VBA in Word 2007, please let me know. So far I found nothing of real value...

I don't know the answer to either of those, but would be interested to know.

fumei
06-28-2010, 11:45 AM
"As well, how would I search for a character? Say I want to know how many times "." is used in a document, what function can I use? "


Answered.

zaboo9
06-28-2010, 11:52 AM
Found it. Thanks for the heads up, Gerry!

MonteCristo
06-28-2010, 09:09 PM
thanks Gerry, I solved my issues.