PDA

View Full Version : Count number of words each sentence



swaggerbox
07-01-2014, 12:25 AM
In a file I have multiple paragraphs. I would like to count the number of words in EACH sentence of a paragraph. How do I do that? Can anyone help to get me started?

Paragraph1
Sentence 1: 30 words
Sentence 2: 15 words
Sentence 3: 20 words

Paragraph2
Sentence 1: 22 words
Sentence 2: 14 words
Sentence 3: 20 words
Sentence 4: 10 words

Should not include space in the word count

macropod
07-01-2014, 03:32 AM
Word can't really do that intelligently. Although there is a VBA method for counting sentences, a sentence in VBA has nothing to do with grammar. For example, consider the following:

Mr. Smith went to Dr. John's Grocery Store, to buy 1kg of Mrs. Green's Mt. Pleasant macadamia nuts.

For you and me, that would probably count as one sentence; for VBA it counts as 5...

swaggerbox
07-01-2014, 03:40 AM
Hi Pau,

Is there a way I could read each sentence (msgbox) and count the number of words (should not include space)?

swaggerbox
07-01-2014, 03:54 AM
For example, the code below reads all sentences in a file. How do I add a line that includes the word count (does not include space)



Public Sub ParseDoc()
Dim paras As Paragraphs
Dim para As Paragraph
Dim sents As Sentences
Dim sent As Range




Set paras = ActiveDocument.Paragraphs
For Each para In paras
Set sents = para.Range.Sentences
For Each sent In sents
MsgBox (sent.Text)
Next
Next
End Sub

macropod
07-01-2014, 03:55 AM
You don't seem to be paying attention. Please read my previous post.

As described in my previous post, for VBA, 'Mr.' is a sentence. So is 'Smith went to Dr.' and 'John's Grocery Store, to buy 1kg of Mrs.' and '' and 'Green's Mt.' and 'Pleasant macadamia nuts.'. I doubt that's much use to anyone.

swaggerbox
07-01-2014, 03:58 AM
so you're saying we couldn't get an accurate count based on the example you posted above?

macropod
07-01-2014, 07:00 AM
so you're saying we couldn't get an accurate count based on the example you posted above?
Well, if you think 5 as the sentence count for the example given is accurate, the answer is yes; otherwise, it's no.