une


Does there exist a predefined function that counts the number of words in a string



Re: Counting words

Andy Pope


Hi,

You do not specify which program you want to do this in.
For excel you can create your own.

Formula, assuming word count of text in cell A1.
=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1

VBA function.
Sub Test()

Dim strTest As String

strTest = "Does there exist a predefined function that counts the number of words in a string "
MsgBox WordCount(strTest) & " words in the sentence" & vbLf & strTest, vbInformation

End Sub

Function WordCount(Text As String) As Long
' xl version 2000 or higher required for Split function
WordCount = UBound(Split(Text, " ")) + 1
End Function







Re: Counting words

une

Thanks, I am using MS Word 2002. I am still trying to decipher the code you posted, but is seems that you can make use of the MS Word "Word Count" tool within a VBA procedure. This tool is invoked a different way than one would invoke a pre-defined VBA function. Is this correct

I would like to apply the MS Word "Word Count" tool to Selection (text highlighted by user) within a VBA procedure. Is this possible

PS - what are vbLf and vbInformation and why did you use them






Re: Counting words

Andy Pope

With word you can use the Words collection.

This will display a word count of the currently selected text.

MsgBox Selection.Words.Count






Re: Counting words

une

Thanks, I will give it a try now.

Collections I seem to have discovered another corner of VBA, my quest continues.





Re: Counting words

une

That is great, but when Word Count is used in a macro it counts incorrectly sometimes.

Here is the code I used;

Private Sub Label1_Click()
MsgBox Selection.Words.Count
End Sub

Then I added this text to the previously blank document;

And I went to the park with my brother

When I use the standard Word Count tool in the toolbar to count the number of words in the document, it returns the correct answer, which is 9. When I highlight all of this text and run my macro to count the words, the incorrect answer of 10 is returned. Upon closer inspection I notice that when I quickly highlighted this text prior to executing the macro, the highlighting block extended one blank space beyond the last visible character (the letter r). So I then carefully reselected all the text, this time backing the highlighting block back one space so it finished right on the last character (which is the letter r). I then ran the macro again and voila, the correct answer of 9 was returned. Furthermore, if no text is highlighted prior to running the macro, an answer of 1 is returned, which is incorrect. How can I eliminate this anomoly





Re: Counting words

une

I also notice that for example it counts an isolated period as a word. It does not seem like a very sophisticated tool. I may have to create one myself.

I also notice that it counts non visible "return" characters as words. This tool is not looking very useful at the moment, but I am happy that you showed me how to implement it.





Re: Counting words

une

I did some experimenting with the Count "functions" under the Words, Sentences and Paragraphs collections. They all calculate incorrectly much of the time. I must be doing something wrong, these functions can't be that flawed.



Re: Counting words

Andy Pope

The help says

"Note Punctuation and paragraph marks in a document are included in the Words collection."

So that would explain the variation.

Try this instead.

Sub x()

Dim dlgX As Dialog

MsgBox Selection.Words.Count
Set dlgX = Application.Dialogs(wdDialogToolsWordCount)
MsgBox dlgX.Words

End Sub






Re: Counting words

une

Thanks again. Using the same example of text shown above, running the code you suggested resulted in 2 message boxes being displayed in succession. The first said there were 10 words and the second said there were 5 words. There are in fact 9 words. I am also trying to figure this out.





Re: Counting words

une

I am getting there using regular expressions. Thanks for your help.