How do you count the number of words in a sentence and the number of sentences in a paragraph?

C

Charlie Mac

I need to count the number of words in a sentence and the number of
sentences in each paragraph in a document? Please help.

Thank you in advance.
 
H

Helmut Weber

Hi Charlie,

like this:

Sub test002()
Dim rDcm As Range ' the documents main story range
Dim oWrd As Object ' a word
Dim oSnt As Object ' a sentence
Dim oPrg As Paragraph ' a paragraph
Dim lWrd As Long ' a counter for words
Dim lSnt As Long ' a counter for sentences
Dim lPrg As Long ' a counter for paragraphs

Set rDcm = ActiveDocument.Range

For Each oPrg In rDcm.Paragraphs
lPrg = lPrg + 1
lSnt = 0
For Each oSnt In oPrg.Range.Sentences
lSnt = lSnt + 1
lWrd = 0
For Each oWrd In oSnt.Words
lWrd = lWrd + 1
Next
Next
Debug.Print lPrg, lSnt, lWrd
' paragraph, sentence, words
Next
End Sub

I'm hoping it's understood,
that "word" and "sentence" are human concepts,
and therefore fuzzy,
which means, if there is no clear definition,
there is no clear result.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jean-Guy Marcil

Charlie Mac was telling us:
Charlie Mac nous racontait que :
I need to count the number of words in a sentence and the number of
sentences in each paragraph in a document? Please help.
Try this tio get you going:
'_______________________________________
Dim SelectedSentenceRge As Range
Dim SelectedParaRge As Range
Dim WordCountLng As Long
Dim SentCountLng As Long

Set SelectedSentenceRge = Selection.Range.Sentences(1)
'-1 because Word counts the final punctuation as a word.
WordCountLng = SelectedSentenceRge.Words.Count - 1
MsgBox WordCountLng
'or, in one line if you do not need the sentence range
'or word count elsewhere:
MsgBox Selection.Range.Sentences(1).Words.Count - 1

Set SelectedParaRge = Selection.Range.Paragraphs(1).Range
SentCountLng = SelectedParaRge.Sentences.Count
MsgBox SentCountLng
'or, in one line if you do not need the paragraph range
'or sentence count elsewhere:
MsgBox Selection.Range.Paragraphs(1).Range.Sentences.Count
'_______________________________________

For the total number of sentences per paragraph in the document, you need a
loop and a array.




o.caTHISTOO
Word MVP site: http://www.word.mvps.org
 
C

Charlie Mac

Helmut,

Thank you for your assistance. It took me a while to walk through
your code, but it was very helpful. I am trying to place two comments
in the code below, but I am new to VBA and don't know the syntax to
select the sentence or paragraph (see attempt below). Your
suggestions are appreciated. Thanks.

Charlie from Texas


Hi Charlie,

like this:

Sub test002()
Dim rDcm As Range ' the documents main story range
Dim oWrd As Object ' a word
Dim oSnt As Object ' a sentence
Dim oPrg As Paragraph ' a paragraph
Dim lWrd As Long ' a counter for words
Dim lSnt As Long ' a counter for sentences
Dim lPrg As Long ' a counter for paragraphs

Set rDcm = ActiveDocument.Range

For Each oPrg In rDcm.Paragraphs
lPrg = lPrg + 1
lSnt = 0
For Each oSnt In oPrg.Range.Sentences
lSnt = lSnt + 1
lWrd = 0
For Each oWrd In oSnt.Words
lWrd = lWrd + 1
Next
'Selection.Comments.Add Range:=Selection.Range,
Text:="Sentence too long."
If ISnt = 1 Then Selection.Comments.Add
Range:=Selection.Range, Text:="One sentence paragraph"
 
C

Charlie Mac

Jean-guy,

Thank you for your response. I learned several valuable tidbits from
your code and I didn't know that Word counted the period.... I am
new to VBA and presently studying syntax to place comments on selected
paragraphs and sentences.

Take care,

Charlie from Texas
 
H

Helmut Weber

Hi Charlie,

even for adding comments you don't need the selection.object.
As I don't quite understand what you want to do exactly,
here comes an example for what I think comes near.

Note, that you can't add a comment to the last paragraph in a doc
in the way as to other 1-letter-words. The comment signs,
the parentheses, will not include the last paragraph mark.

Sub test002()
Dim rDcm As Range ' the documents main story range
Dim oWrd As Object ' a word
Dim oSnt As Object ' a sentence
Dim oPrg As Paragraph ' a paragraph
Dim lWrd As Long ' a counter for words
Dim lSnt As Long ' a counter for sentences
Dim lPrg As Long ' a counter for paragraphs

Set rDcm = ActiveDocument.Range

For Each oPrg In rDcm.Paragraphs
lPrg = lPrg + 1
lSnt = 0
For Each oSnt In oPrg.Range.Sentences
If Len(oSnt) > 25 Then
oSnt.Comments.Add _
Range:=oSnt, _
Text:="sentence too long"
End If
lSnt = lSnt + 1
lWrd = 0
For Each oWrd In oSnt.Words
lWrd = lWrd + 1
If Len(oWrd) = 1 Then
oWrd.Comments.Add _
Range:=oWrd, _
Text:="1 character word"
End If
Next
Next
' Debug.Print lPrg, lSnt, lWrd
' paragraph, sentence, words
Next
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
C

Charlie Mac

Helmut of Bavaria,

Thank you for your enlightening code. I am trying to scan a document
and comment sentences that are too long and paragraphs that contain
only one sentence. Thanks to you, all is working except the last
comment that is generated by one-sentence paragraphs. It generates a
"method of data member not found" error, which is an easy fix for
everyone except me. ;-)

Take care,

Charlie of Texas

Set myrange = Selection.Range
myrange.WholeStory ' the documents main story range
Dim aWord As Object ' a word
Dim aSent As Object ' a sentence
Dim aPara As Paragraph ' a paragraph
Dim WordCnt As Integer ' a counter for words
Dim SentCnt As Integer ' a counter for sentences
Dim ParaCnt As Integer ' a counter for paragraphs
Dim NewRange As Paragraph
Dim Eye As Integer
For Each aPara In myrange.Paragraphs
ParaCnt = ParaCnt + 1
SentCnt = 0
For Each aSent In aPara.Range.Sentences
SentCnt = SentCnt + 1 ' sentence counter
WordCnt = -1 ' Word counts the period as a word
For Each aWord In aSent.Words
WordCnt = WordCnt + 1 ' word counter
Next
If WordCnt > 30 Then aSent.Comments.Add Range:=aSent,
Text:="This sentence is too long. It contains more than 30 words."
Next
If SentCnt = 1 Then aPara.Comments.Add Range:=aPara,
Text:="Avoid one sentence paragraphs"
Next
 
C

Charlie Mac

Helmut from Bavaria,

I finally got the code to work. I was leaving out "range". The
simple things.... Thank you for your help.

Take care,

Charlie from Texas
 

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