Counting number of words in a sentence and highlighting or commenting

Hi,

I found some old code, from Helmut of Bavaria I believe, that is
supposed to find long sentences in a document:

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) > 50 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

I ran this macro and it seems to comment *every* sentence, regardless
of the length. Does anyone know why? Has anyone improved on this code?
I'd like to find long sentences in a document and consider revising
them. Either commenting or highlighting them would be fine.

Thanks, Bruce
0
BHW
3/4/2010 1:48:40 PM
word.vba.general 1023 articles. 1 followers. Follow

7 Replies
3843 Views

Similar Articles

[PageSpeed] 52

Bruce,

Yes that is Helmut's code and it works (as well as something like this 
works) here.  Are you sure that all over your sentences are not over 50 
characters in length (that includes everything: spaces, punctuation, 
characters)

Improved on?  Not really.  It can be shortened:

Sub test002()
Dim rDcm As Range      ' the documents main story range
Dim oSnt As Object     ' a sentence
Dim oPrg As Paragraph  ' a paragraph
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
    If Len(oSnt) > 15 Then
      oSnt.Comments.Add oSnt, "sentence too long"
      Debug.Print lPrg, lSnt
    End If
  Next
Next
End Sub

Part of the problem is Word's misconception of "Sentence."  Consider the 
following three paragraphs each containing 1 sentence:


I came, I saw.

I came, I saw, I conquered.

Mr. X came, Mr. X saw, Mr. X conqoured.


If I set the condition to > 15 and run the code, I would expect that both 
the second and third sentences would be commented.  Sentence 2 is, but 
sentence 3 is not.  Why?  Sentence 3 is clearly longer than sentence 2.  The 
reason is that Word treats every "." used in abbreviations as a sentence 
stop.  You can see this by running the following code:

MsgBox ActiveDocument.Range.Sentences.Count

While there are only 3 sentences as you and I understand them, the return is 
6 because Word counts every "." stop.



"BHW" <brucehughw@gmail.com> wrote in message 
news:d3fe028d-d661-4ac1-9e67-341d2d2b9757@b30g2000yqd.googlegroups.com...
> Hi,
>
> I found some old code, from Helmut of Bavaria I believe, that is
> supposed to find long sentences in a document:
>
> 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) > 50 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
>
> I ran this macro and it seems to comment *every* sentence, regardless
> of the length. Does anyone know why? Has anyone improved on this code?
> I'd like to find long sentences in a document and consider revising
> them. Either commenting or highlighting them would be fine.
>
> Thanks, Bruce 


0
Greg
3/4/2010 2:55:53 PM
Ah, 50 characters! I am concerned about the number of *words* in a
sentence, not the number of characters, although the latter matters,
too. Is there a way to get the macro to work with the number of words
rather than number of characters? Thanks much, and thanks also for the
pointer re periods.

Bruce

On Mar 4, 9:55=A0am, "Greg Maxey"
<gma...@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote:
> Bruce,
>
> Yes that is Helmut's code and it works (as well as something like this
> works) here. =A0Are you sure that all over your sentences are not over 50
> characters in length (that includes everything: spaces, punctuation,
> characters)
>
> Improved on? =A0Not really. =A0It can be shortened:
>
> Sub test002()
> Dim rDcm As Range =A0 =A0 =A0' the documents main story range
> Dim oSnt As Object =A0 =A0 ' a sentence
> Dim oPrg As Paragraph =A0' a paragraph
> Dim lSnt As Long =A0 =A0 =A0 ' a counter for sentences
> Dim lPrg As Long =A0 =A0 =A0 ' a counter for paragraphs
> Set rDcm =3D ActiveDocument.Range
> For Each oPrg In rDcm.Paragraphs
> =A0 lPrg =3D lPrg + 1
> =A0 lSnt =3D 0
> =A0 For Each oSnt In oPrg.Range.Sentences
> =A0 =A0 lSnt =3D lSnt + 1
> =A0 =A0 If Len(oSnt) > 15 Then
> =A0 =A0 =A0 oSnt.Comments.Add oSnt, "sentence too long"
> =A0 =A0 =A0 Debug.Print lPrg, lSnt
> =A0 =A0 End If
> =A0 Next
> Next
> End Sub
>
> Part of the problem is Word's misconception of "Sentence." =A0Consider th=
e
> following three paragraphs each containing 1 sentence:
>
> I came, I saw.
>
> I came, I saw, I conquered.
>
> Mr. X came, Mr. X saw, Mr. X conqoured.
>
> If I set the condition to > 15 and run the code, I would expect that both
> the second and third sentences would be commented. =A0Sentence 2 is, but
> sentence 3 is not. =A0Why? =A0Sentence 3 is clearly longer than sentence =
2. =A0The
> reason is that Word treats every "." used in abbreviations as a sentence
> stop. =A0You can see this by running the following code:
>
> MsgBox ActiveDocument.Range.Sentences.Count
>
> While there are only 3 sentences as you and I understand them, the return=
 is
> 6 because Word counts every "." stop.
>
> "BHW" <brucehu...@gmail.com> wrote in message
>
> news:d3fe028d-d661-4ac1-9e67-341d2d2b9757@b30g2000yqd.googlegroups.com...
>
> > Hi,
>
> > I found some old code, from Helmut of Bavaria I believe, that is
> > supposed to find long sentences in a document:
>
> > Sub test002()
> > Dim rDcm As Range =A0 =A0 =A0' the documents main story range
> > Dim oWrd As Object =A0 =A0 ' a word
> > Dim oSnt As Object =A0 =A0 ' a sentence
> > Dim oPrg As Paragraph =A0' a paragraph
> > Dim lWrd As Long =A0 =A0 =A0 ' a counter for words
> > Dim lSnt As Long =A0 =A0 =A0 ' a counter for sentences
> > Dim lPrg As Long =A0 =A0 =A0 ' a counter for paragraphs
>
> > Set rDcm =3D ActiveDocument.Range
>
> > For Each oPrg In rDcm.Paragraphs
> > =A0 lPrg =3D lPrg + 1
> > =A0 lSnt =3D 0
> > =A0 For Each oSnt In oPrg.Range.Sentences
> > =A0 =A0 =A0 =A0 If Len(oSnt) > 50 Then
> > =A0 =A0 =A0 =A0 =A0 =A0oSnt.Comments.Add _
> > =A0 =A0 =A0 =A0 =A0 =A0Range:=3DoSnt, _
> > =A0 =A0 =A0 =A0 =A0 =A0Text:=3D"sentence too long"
> > =A0 =A0 =A0 =A0 End If
> > =A0 =A0 =A0lSnt =3D lSnt + 1
> > =A0 =A0 =A0lWrd =3D 0
> > ' =A0 =A0 =A0For Each oWrd In oSnt.Words
> > ' =A0 =A0 =A0 =A0 lWrd =3D lWrd + 1
> > ' =A0 =A0 =A0 =A0 If Len(oWrd) =3D 1 Then
> > ' =A0 =A0 =A0 =A0 =A0 =A0oWrd.Comments.Add _
> > ' =A0 =A0 =A0 =A0 =A0 =A0Range:=3DoWrd, _
> > ' =A0 =A0 =A0 =A0 =A0 =A0Text:=3D"1 character word"
> > ' =A0 =A0 =A0 =A0 End If
> > ' =A0 =A0 =A0Next
> > =A0 Next
> > =A0 Debug.Print lPrg, lSnt, lWrd
> > =A0 ' paragraph, sentence, words
> > Next
> > End Sub
>
> > I ran this macro and it seems to comment *every* sentence, regardless
> > of the length. Does anyone know why? Has anyone improved on this code?
> > I'd like to find long sentences in a document and consider revising
> > them. Either commenting or highlighting them would be fine.
>
> > Thanks, Bruce

0
BHW
3/4/2010 4:15:59 PM
Sort of, but with the some of the stame issues.  Word counts commas, 
periods, and other non-word things as words.  You might use something like:

Sub test002()
Dim rDcm As Range      ' the documents main story range
Dim oSnt As Object     ' a sentence
Dim oPrg As Paragraph  ' a paragraph
Dim lSnt As Long       ' a counter for sentences
Dim lPrg As Long       ' a counter for paragraphs
Dim oWord As Range
Dim Cnt As Long
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
    Cnt = 0
    For Each oWord In oSnt.Words
       If Not InStr("., ?!" & Chr(11) & Chr(13), oWord) <> 0 Then
         Cnt = Cnt + 1
       End If
    Next oWord
    If Cnt > 4 Then
      oSnt.Comments.Add oSnt, "sentence too long"
      Debug.Print lPrg, lSnt
    End If
  Next
Next
End Sub


BHW wrote:
> Ah, 50 characters! I am concerned about the number of *words* in a
> sentence, not the number of characters, although the latter matters,
> too. Is there a way to get the macro to work with the number of words
> rather than number of characters? Thanks much, and thanks also for the
> pointer re periods.
>
> Bruce
>
> On Mar 4, 9:55 am, "Greg Maxey"
> <gma...@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote:
>> Bruce,
>>
>> Yes that is Helmut's code and it works (as well as something like
>> this works) here. Are you sure that all over your sentences are not
>> over 50 characters in length (that includes everything: spaces,
>> punctuation, characters)
>>
>> Improved on? Not really. It can be shortened:
>>
>> Sub test002()
>> Dim rDcm As Range ' the documents main story range
>> Dim oSnt As Object ' a sentence
>> Dim oPrg As Paragraph ' a paragraph
>> 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
>> If Len(oSnt) > 15 Then
>> oSnt.Comments.Add oSnt, "sentence too long"
>> Debug.Print lPrg, lSnt
>> End If
>> Next
>> Next
>> End Sub
>>
>> Part of the problem is Word's misconception of "Sentence." Consider
>> the following three paragraphs each containing 1 sentence:
>>
>> I came, I saw.
>>
>> I came, I saw, I conquered.
>>
>> Mr. X came, Mr. X saw, Mr. X conqoured.
>>
>> If I set the condition to > 15 and run the code, I would expect that
>> both the second and third sentences would be commented. Sentence 2
>> is, but sentence 3 is not. Why? Sentence 3 is clearly longer than
>> sentence 2. The reason is that Word treats every "." used in
>> abbreviations as a sentence stop. You can see this by running the
>> following code:
>>
>> MsgBox ActiveDocument.Range.Sentences.Count
>>
>> While there are only 3 sentences as you and I understand them, the
>> return is 6 because Word counts every "." stop.
>>
>> "BHW" <brucehu...@gmail.com> wrote in message
>>
>> news:d3fe028d-d661-4ac1-9e67-341d2d2b9757@b30g2000yqd.googlegroups.com...
>>
>>> Hi,
>>
>>> I found some old code, from Helmut of Bavaria I believe, that is
>>> supposed to find long sentences in a document:
>>
>>> 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) > 50 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
>>
>>> I ran this macro and it seems to comment *every* sentence,
>>> regardless of the length. Does anyone know why? Has anyone improved
>>> on this code? I'd like to find long sentences in a document and
>>> consider revising them. Either commenting or highlighting them
>>> would be fine.
>>
>>> Thanks, Bruce 


0
Greg
3/4/2010 4:54:54 PM
Greg:  " Word counts commas, periods, and other non-word things as words. "

Technically speaking, this is not completely accurate.

Word itself does NOT count commas, periods etc as words.  It has a built-in
delimiter function.

VBA does count those as "words".

If you have:

Mr. X came, Mr. X saw, Mr. X conqoured.

and you use Tools > Word Count, you get a word count of 9.

Mr.
X
came
Mr.
X
saw
Mr.
X
conquored

As does (assuming it is Paragraph(4)): 

Dim r As Range
Set r = ActiveDocument.Paragraphs(4).Range
MsgBox r.ComputeStatistics(wdStatisticWords) - shows as 9

HOWEVER,

Set r = ActiveDocument.Paragraphs(4).Range
MsgBox r.Words.Count

gives 16.

1  Mr
2  .
3  X
4  came
5  ,
6  Mr
7  .
8  X
9  saw
10  ,
11  Mr
12  .
13  X
14  conquored
15  .
16  <p>   a paragraph mark

So, going through one route (the menu route in Word itself) returns the
"right" number.  As does .ComputeStatistics.  Both use an internal delimiter
function to extract the commas. periods etc.  A straight VBA Word.Count does
not use those internal delimiters.

Greg Maxey wrote:
>Sort of, but with the some of the stame issues.  Word counts commas, 
>periods, and other non-word things as words.  You might use something like:
>
>Sub test002()
>Dim rDcm As Range      ' the documents main story range
>Dim oSnt As Object     ' a sentence
>Dim oPrg As Paragraph  ' a paragraph
>Dim lSnt As Long       ' a counter for sentences
>Dim lPrg As Long       ' a counter for paragraphs
>Dim oWord As Range
>Dim Cnt As Long
>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
>    Cnt = 0
>    For Each oWord In oSnt.Words
>       If Not InStr("., ?!" & Chr(11) & Chr(13), oWord) <> 0 Then
>         Cnt = Cnt + 1
>       End If
>    Next oWord
>    If Cnt > 4 Then
>      oSnt.Comments.Add oSnt, "sentence too long"
>      Debug.Print lPrg, lSnt
>    End If
>  Next
>Next
>End Sub
>
>> Ah, 50 characters! I am concerned about the number of *words* in a
>> sentence, not the number of characters, although the latter matters,
>[quoted text clipped - 104 lines]
>>>
>>>> Thanks, Bruce

-- 
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201003/1

0
Fumei2
3/4/2010 8:24:50 PM
This is also confusing with Sentences.

Mr. X came, Mr. X saw, Mr. X conquered. Mr. X came, Mr. X saw, Mr. X
conquered. Mr. X came, Mr. X saw, Mr. X conquered.

We, as humans, can parse that as THREE sentences.  Word can not.  That is why
there is no Sentences count with Tools > Word Count.  Paragraphs, yes.  Lines,
yes.  Words, yes.  But not Sentences.

If you use VBA to get a Sentences.Count, it = 12.

Mr.
X came, Mr.
X saw, Mr.
X conquered.
Mr.
X came, Mr.
X saw, Mr.
X conquered.
Mr.
X came, Mr.
X saw, Mr.
X conquered.

This should emphasize something a surprising number of people appear to miss.
Word does not "understand", or "know" ANYTHING about the text in the document.


"Understanding" and "askdgakhergwk" are exactly the same - in terms of
"meaning".

The only "meaning" comes from algorthims built up to do specific tasks.  And
even with that there is still no real meaning.  After all, if you put
"askdgakhergwk" into the dictonary Word will accept it as a proper word.

I have had a number of people honestly surprised that an input of "Mrr. Jogn
Glenn" in a text formfield (or a textbox on a userform) is accepted.  They
say it does not "make sense".  Hmmmm.  Except Word has no sense.  ALL text is
just that...text; ASCII characters.  One at a time.

You can, of course, get the "right" Sentence count - 3 - from (again assuming
it is Paragraph(4)):

Mr. X came, Mr. X saw, Mr. X conquered. Mr. X came, Mr. X saw, Mr. X
conquered. Mr. X came, Mr. X saw, Mr. X conquered.

Sub MySentenceCount()
Dim r As Range
Dim oSnt
Dim counter As Long

Set r = ActiveDocument.Paragraphs(4).Range

For Each oSnt In r.Sentences
   If Left(oSnt, 2) <> "Mr" Then
      If Right(oSnt, 4) <> "Mr. " Then
         counter = counter + 1
      End If
   End If
Next
MsgBox counter  ' displays 3
End Sub

but only by hard-coding the logic regarding "Mr."

Fumei2 wrote:
>Greg:  " Word counts commas, periods, and other non-word things as words. "
>
>Technically speaking, this is not completely accurate.
>
>Word itself does NOT count commas, periods etc as words.  It has a built-in
>delimiter function.
>
>VBA does count those as "words".
>
>If you have:
>
>Mr. X came, Mr. X saw, Mr. X conqoured.
>
>and you use Tools > Word Count, you get a word count of 9.
>
>Mr.
>X
>came
>Mr.
>X
>saw
>Mr.
>X
>conquored
>
>As does (assuming it is Paragraph(4)): 
>
>Dim r As Range
>Set r = ActiveDocument.Paragraphs(4).Range
>MsgBox r.ComputeStatistics(wdStatisticWords) - shows as 9
>
>HOWEVER,
>
>Set r = ActiveDocument.Paragraphs(4).Range
>MsgBox r.Words.Count
>
>gives 16.
>
>1  Mr
>2  .
>3  X
>4  came
>5  ,
>6  Mr
>7  .
>8  X
>9  saw
>10  ,
>11  Mr
>12  .
>13  X
>14  conquored
>15  .
>16  <p>   a paragraph mark
>
>So, going through one route (the menu route in Word itself) returns the
>"right" number.  As does .ComputeStatistics.  Both use an internal delimiter
>function to extract the commas. periods etc.  A straight VBA Word.Count does
>not use those internal delimiters.
>
>>Sort of, but with the some of the stame issues.  Word counts commas, 
>>periods, and other non-word things as words.  You might use something like:
>[quoted text clipped - 32 lines]
>>>>
>>>>> Thanks, Bruce

-- 
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201003/1

0
Fumei2
3/4/2010 9:12:12 PM
Fumei2,

Right of course.  "VBA count ..." would have be more correct.

Bonus Tip:  If one learns to apply it and pay attention to its flags, Word 
will also help improve spelling:  "conqoured"

Original error Greg's of course.

"Fumei2 via OfficeKB.com" <u53619@uwe> wrote in message 
news:a4852f0a12c46@uwe...
> Greg:  " Word counts commas, periods, and other non-word things as words. 
> "
>
> Technically speaking, this is not completely accurate.
>
> Word itself does NOT count commas, periods etc as words.  It has a 
> built-in
> delimiter function.
>
> VBA does count those as "words".
>
> If you have:
>
> Mr. X came, Mr. X saw, Mr. X conqoured.
>
> and you use Tools > Word Count, you get a word count of 9.
>
> Mr.
> X
> came
> Mr.
> X
> saw
> Mr.
> X
> conquored
>
> As does (assuming it is Paragraph(4)):
>
> Dim r As Range
> Set r = ActiveDocument.Paragraphs(4).Range
> MsgBox r.ComputeStatistics(wdStatisticWords) - shows as 9
>
> HOWEVER,
>
> Set r = ActiveDocument.Paragraphs(4).Range
> MsgBox r.Words.Count
>
> gives 16.
>
> 1  Mr
> 2  .
> 3  X
> 4  came
> 5  ,
> 6  Mr
> 7  .
> 8  X
> 9  saw
> 10  ,
> 11  Mr
> 12  .
> 13  X
> 14  conquored
> 15  .
> 16  <p>   a paragraph mark
>
> So, going through one route (the menu route in Word itself) returns the
> "right" number.  As does .ComputeStatistics.  Both use an internal 
> delimiter
> function to extract the commas. periods etc.  A straight VBA Word.Count 
> does
> not use those internal delimiters.
>
> Greg Maxey wrote:
>>Sort of, but with the some of the stame issues.  Word counts commas,
>>periods, and other non-word things as words.  You might use something 
>>like:
>>
>>Sub test002()
>>Dim rDcm As Range      ' the documents main story range
>>Dim oSnt As Object     ' a sentence
>>Dim oPrg As Paragraph  ' a paragraph
>>Dim lSnt As Long       ' a counter for sentences
>>Dim lPrg As Long       ' a counter for paragraphs
>>Dim oWord As Range
>>Dim Cnt As Long
>>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
>>    Cnt = 0
>>    For Each oWord In oSnt.Words
>>       If Not InStr("., ?!" & Chr(11) & Chr(13), oWord) <> 0 Then
>>         Cnt = Cnt + 1
>>       End If
>>    Next oWord
>>    If Cnt > 4 Then
>>      oSnt.Comments.Add oSnt, "sentence too long"
>>      Debug.Print lPrg, lSnt
>>    End If
>>  Next
>>Next
>>End Sub
>>
>>> Ah, 50 characters! I am concerned about the number of *words* in a
>>> sentence, not the number of characters, although the latter matters,
>>[quoted text clipped - 104 lines]
>>>>
>>>>> Thanks, Bruce
>
> -- 
> Message posted via OfficeKB.com
> http://www.officekb.com/Uwe/Forums.aspx/word-programming/201003/1
> 


0
Greg
3/4/2010 9:17:46 PM
In view of Fumei2 valid observations, this may be simplier:

Sub test002()
Dim rDcm As Range      ' the documents main story range
Dim oSen As Range
Dim oPrg As Paragraph  ' a paragraph
Set rDcm = ActiveDocument.Range
For Each oPrg In rDcm.Paragraphs
  For Each oSen In oPrg.Range.Sentences
    If oSen.ComputeStatistics(wdStatisticWords) > 4 Then
      oSen.Comments.Add oSen, "sentence too long"
      Debug.Print lPrg, lSnt
    End If
  Next
Next
End Sub

It will still fall flat if it encounters periods after abbreviations.

Greg Maxey wrote:
> Sort of, but with the some of the stame issues.  Word counts commas,
> periods, and other non-word things as words.  You might use something
> like:
> Sub test002()
> Dim rDcm As Range      ' the documents main story range
> Dim oSnt As Object     ' a sentence
> Dim oPrg As Paragraph  ' a paragraph
> Dim lSnt As Long       ' a counter for sentences
> Dim lPrg As Long       ' a counter for paragraphs
> Dim oWord As Range
> Dim Cnt As Long
> 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
>    Cnt = 0
>    For Each oWord In oSnt.Words
>       If Not InStr("., ?!" & Chr(11) & Chr(13), oWord) <> 0 Then
>         Cnt = Cnt + 1
>       End If
>    Next oWord
>    If Cnt > 4 Then
>      oSnt.Comments.Add oSnt, "sentence too long"
>      Debug.Print lPrg, lSnt
>    End If
>  Next
> Next
> End Sub
>
>
> BHW wrote:
>> Ah, 50 characters! I am concerned about the number of *words* in a
>> sentence, not the number of characters, although the latter matters,
>> too. Is there a way to get the macro to work with the number of words
>> rather than number of characters? Thanks much, and thanks also for
>> the pointer re periods.
>>
>> Bruce
>>
>> On Mar 4, 9:55 am, "Greg Maxey"
>> <gma...@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote:
>>> Bruce,
>>>
>>> Yes that is Helmut's code and it works (as well as something like
>>> this works) here. Are you sure that all over your sentences are not
>>> over 50 characters in length (that includes everything: spaces,
>>> punctuation, characters)
>>>
>>> Improved on? Not really. It can be shortened:
>>>
>>> Sub test002()
>>> Dim rDcm As Range ' the documents main story range
>>> Dim oSnt As Object ' a sentence
>>> Dim oPrg As Paragraph ' a paragraph
>>> 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
>>> If Len(oSnt) > 15 Then
>>> oSnt.Comments.Add oSnt, "sentence too long"
>>> Debug.Print lPrg, lSnt
>>> End If
>>> Next
>>> Next
>>> End Sub
>>>
>>> Part of the problem is Word's misconception of "Sentence." Consider
>>> the following three paragraphs each containing 1 sentence:
>>>
>>> I came, I saw.
>>>
>>> I came, I saw, I conquered.
>>>
>>> Mr. X came, Mr. X saw, Mr. X conqoured.
>>>
>>> If I set the condition to > 15 and run the code, I would expect that
>>> both the second and third sentences would be commented. Sentence 2
>>> is, but sentence 3 is not. Why? Sentence 3 is clearly longer than
>>> sentence 2. The reason is that Word treats every "." used in
>>> abbreviations as a sentence stop. You can see this by running the
>>> following code:
>>>
>>> MsgBox ActiveDocument.Range.Sentences.Count
>>>
>>> While there are only 3 sentences as you and I understand them, the
>>> return is 6 because Word counts every "." stop.
>>>
>>> "BHW" <brucehu...@gmail.com> wrote in message
>>>
>>> news:d3fe028d-d661-4ac1-9e67-341d2d2b9757@b30g2000yqd.googlegroups.com...
>>>
>>>> Hi,
>>>
>>>> I found some old code, from Helmut of Bavaria I believe, that is
>>>> supposed to find long sentences in a document:
>>>
>>>> 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) > 50 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
>>>
>>>> I ran this macro and it seems to comment *every* sentence,
>>>> regardless of the length. Does anyone know why? Has anyone improved
>>>> on this code? I'd like to find long sentences in a document and
>>>> consider revising them. Either commenting or highlighting them
>>>> would be fine.
>>>
>>>> Thanks, Bruce 


0
Greg
3/5/2010 12:46:08 AM
Reply:

Similar Artilces:

How do I make numbers become text?
I am trying to create a spreadsheet where numbers entered in one location become text in another. I tried the Help option, but I am still lost. Please help, and thank you. From earlier postings: How to Convert a Numeric Value into English Words http://support.microsoft.com/default.aspx?scid=KB;EN- US;140704& and: (courtesy of a cut and paste from a Tom Ogilvy post): If you want an addin that provides a worksheet function that does this, download Laurent Longre's free morefunc.xll addin found here: http://longre.free.fr/english/ It is downloaded in a zip file which also conta...

Excel
Is there a way to change an imported number such as: 7042626598 and display it as a phone number like: (704) 262-6598 ? Thank you, Be -- Message posted from http://www.ExcelForum.com Ben, Yes you can format the cells containing the 10 digit number to a phone number. Highlight all the required cell and on the Main menu choose Format/Cells/Number/Special/Phone Number Charlie O'Neill >-----Original Message----- >Is there a way to change an imported number such as: > >7042626598 and display it as a phone number like: > >(704) 262-6598 > >? > > >...

Miscellaneous words appear in Excel 2002 cells
I am working on an Excell 2002 workbook and when I put my cursor in a cell , spurious words get entered into the cell e.g. "and thee the and ...." is one example. I have cable modem and it feels like "someone" has taken control of my PC - by the way I have also noticed this hapening in MS Word but is not as bad as Excel - can some one help. thanks KK Have you enabled speech tools? Click Tools / Speech and examine your options. /i. "KK" <anonymous@discussions.microsoft.com> wrote in message news:061d01c39d1c$36fc85f0$a601280a@phx.gbl... > I am w...

Where is the Keep Text Formatting feature located in Word 07
I believe this Keep Text Formatting feature might be what I need, but I have been unable to locate exactly where it is located in Word 2007. I'm trying to rid a Word document sent to me of tables, text boxes, graphics and all other document formatting, while retaining the document's text content. It is unimportant to me whether the text formatting is retained or not. Thanks. Are you referring to a Keep Text Formatting feature in an earlier version of Word? I wonder whether what you're looking for is "Paste Unformatted," since you seem to be saying you _don...

Item level revision numbers on POs
The Item Engineering info on inventory items in manufacturing includes revision numbers for items, however because that info is in the manufacturing dictionary we can't include through report writer on POs. I'd like to develop a routine that would copy the revision number from the item engineering table to one of the user-defined tables in inventory. Any suggestions on how to do that? Or an alternate approach to the problem? -- Jim@TurboChef Jim- This script will put the current Rev level from Item Engineering into User Category 1 on the item master. update iv00101 set usca...

Conver Numbers to Log 10
How do I convert column of numbers (n=3000) to log 10 column of numbers? Assuming that your column is column A, one way: In an unused column, enter =LOG(A1) and copy down 3000 cells. Copy the column, choose column A and choose Edit/Paste Special, selecting the Values radio button. In article <0CDE6301-FC9C-488C-9D9F-3AD26B46EFCE@microsoft.com>, "Fishfan" <Fishfan@discussions.microsoft.com> wrote: > How do I convert column of numbers (n=3000) to log 10 column of numbers? ...

error numbers
Does anyone know if microsoft has a way to look up errors and their resolution by ERROR NUMBER? I have never found anything like that. Now they have something called "Events and Errors Message Center" but even that doesn't appear to let one look up errors by error number! Thanks Hi, Fredrated schrieb: > Does anyone know if microsoft has a way to look up errors and their > resolution by ERROR NUMBER? > > I have never found anything like that. Now they have something called > "Events and Errors Message Center" but even that doesn't appear to...

Excel and Word #2
I converted a class schedule from Word to Excel in order to better work with formulas since I am terrible with math. My concern now is that I will want to email it back to volunteers for review. Can I just copy it into Word and will they be able to view it or will they have to have Excel? Thanks for information. Susan Hello Susan, A copy/paste command will create a Word table in Word from Excel. you could also do the following: Insert, object, From file, browse to the Excel file, Insert. With this method you can check "link" and from now on everytime you change something...

What is the maximum number of users that can share Excel File?
According to the Excel Help file, all users on your network, unless you restrict the number. "pfine" wrote: > ...

Word 2008
Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Intel I have created several files in Word 2008 using templates (.dot) files previously used with Word 2003/2007. Unfortunately, my customer keeps receiving an error when opening the new file I create. The new file (created in .doc format) has some hidden data that is pointing to the template folder on my Mac. The error looks like: Could not open: 'user:Libarary:Application Support:Microsoft:Offce:User Templates:template.dot My customer can fix this error by using the remove hidden data tool inside Office 2003, but that&...

counting using multiple criteria
Hi, I have a problem for which I can't seem to find a working solution. On the one hand I have a vertical table with employee names, the number of years they have been with the company, and all this sorted by the employee's age (in years). This table will be updated from time to time, and as such it doesn't have a fixed length. On the other hand I have a table which shows the number of employees in a particular age category (-20, 21-25, 26-30, etc) horizontally, and the number of years they have been with the company (again in groups: 0-5 yrs, 5-10 yrs, etc) vertically. ...

lookup row number
Hi, can you tell me how to lookup the row number of a name in a list. EG say I have a list of names as below: Andrew Mike Steve Ian Paul I would normally just add a column with the row numbers and then lookup the row number in the second column but is it possible to do without adding the extra column by using the row formula someohow? Thanks Hi Michael Try the below.. =MATCH("Mike",A:A,0) -- Jacob (MVP - Excel) "Michael" wrote: > Hi, > can you tell me how to lookup the row number of a name in a list. > > EG say I hav...

Size of single bracket in Equation (Word 2007)
Hi group, I'd like to change the size of a single bracket in an equation - how can I achieve this? For example, in Latex I could do something like this: F\biggr|_{a} , where biggrl gives me a large sized "|" ... Cheers -Ralf as a work-around I came up with a trick: I used a single Matrix column with 3 rows - inserted the single "|" on the right-hand side - I put the actual equation in the centre of the matrix field and the other matrix fields I left empty ... the result looks pleasing Cheers -Ralf On Feb 1, 1:15=A0pm, "ralf.schaa" ...

Maximum number of bits
I have a requirement to find the maximum number of consecutive zeros in a row in a 32 bit binary number. For example 1111 1111 1111 1000 1111 1111 0000 1111 = 4 1111 1110 1111 1110 1111 1111 1110 1111 = 1 I am actually storing the number in a BIGINT for simplicity. I can do this in a udf with a look fairly easily but was wondering if there is something more sql that will do the job. Cheers, Michael That's an interesting question. Does it include leading zeroes? 0000 0000 0111 1000 1111 1111 0000 1111 = 9? -- Thanks Michael Coles SQL Server MVP ...

Amount in Words Complete Right Formula
give me complete formula See http://www.xldynamic.com/source/xld.xlFAQ0004.html -- HTH Bob Phillips (replace xxxx in the email address with gmail if mailing direct) "Naveed" <Naveed@discussions.microsoft.com> wrote in message news:29B7C0D6-1DED-4321-B1EB-CA6DDCE73852@microsoft.com... > give me complete formula > ...

How do you open PDF files with Word 2007?
How do you open a PDF file in Word 2007? anybody? I tried to download the PDF add on app but that is not working either. I have on line classes I cant complete until I figure out how to do this. You can't open pdf's in Word -- that's the point of pdf files. On Feb 10, 3:06=A0pm, terrycwilson <terrycwil...@discussions.microsoft.com> wrote: > How do you open a PDF file in Word 2007? anybody? I tried to download the= PDF > add on app but that is not working either. I have on line classes I cant > complete until I figure out how to do this. You can'...

can one cell contain more than one independent number
A cell can contain several numbers, separated by space characters or line breaks (Alt+Enter). However, if you want to perform calculations on the numbers, it's best to keep them in separate cells. John K wrote: -- Debra Dalgleish Excel FAQ, Tips & Book List http://www.contextures.com/tiptech.html there's also the option a using matrix formula (list of data) for example you may enter ={1;2;3} in a single cell or selection 'and validate wih CTRL+SHIFT+ENTER but the manipulation of this kinda of formula need to read more about it... ...

finding common numbers in large lists
Hello, I have two large lists of numbers (on two seperate worksheets). By large, I mean that one list is 1000+ numbers, and the second list is 3000+ numbers (each number is in its own cell). The numbers are of the form: xxxx.xxx (that is, they are precise up to three places after the decimal) 1) Is there some quick way that I can find the numbers that are common to both lists? 2) Is there a way I can find the numbers in common to both lists using only the whole number portion (diregarding what comes after the decimal of each number)? Thanks! Hi see: http://www.cpearson.com/excel/duplic...

MS Word file extension changed through e-mail
MS Word .docx files sent as attachments are received without the x, as a .doc instead. The same is true for MS Excel .xlsx changed to .xls Speak firmly to whoever set up the email system. They have not applied their updates. When the file goes through, it is being recognised as a Microsoft Word file, but the extension is being wrongly changed. Some free-mail web services have a bad habit of doing this. It's not a Word problem, the change is being made either by the sending email program or the receiving email program. Cheers On 24/02/10 2:32 PM, in article 2e082f5...

creating a multilevel numbering sysem
I'm trying to create a multileve numbering system ex: 100, 100.1, 100.1.1, 100.2, 200, 200.1, 2001.1, 200.2, and so on. I would like to have at least 3-4 tiers under each number that would automatically number. And you want this in Publisher? I don't think so... -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "v" <v@discussions.microsoft.com> wrote in message news:C1F59342-9F2F-4E91-BE27-0BA992EB17BE@microsoft.com... > I'm trying to create a multileve numbering system > ex: 100, 100.1, 100.1.1, ...

extracting numbers from a text field and applying formula
here's my question... i have a cell that has a text field of this type: '9-19' (a-b) I want to create a formula that will subtract 19-9 (b-a) and return the result as a number// can anyone help?! is this even doable? I'd really appreciate it! PS also, i'm very fairly new at excel so a detailed response with th formula would really help! ALe ----------------------------------------------- ~~ Message posted from http://www.ExcelTip.com ~~View and post usenet messages directly from http://www.ExcelForum.com Try: =MID(A1,FIND("-",A1)+1,99)-LEFT(A1,FIND("...

Imported Numbers not recognized as numbers
Hi there -- I've seen this problem posted a lot before, however haven't yet seen a solution posted that works for my situation. This applies to Office XP... So I copy a column of data (numbers) from a website and paste it int Excel, but the numbers are not recognized as numeric values (can't su them, etc.) I have tried all of the "easy" solutions -- changing their format t general or number, copying a 1 from another cell in a clean workboo and using the paste special/multiply (or a 0 and paste special/add function...I've tried the text to columns thing -- basicall...

Call a word doc
Currently I am using our DB to track bids to general contractors for building materials. Each bid proposal is prepared in MS Word. Is there a way to call a Word doc for reference only within a form, for example using a button? The proposal could be either a .doc or .pdf that could be reprinted or viewed from Access... -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/access-formscoding/200802/1 Whoops, that was not complete. Each bid has its own word file. So when I click a button I need to be able to define the file name. We use a bid number formatted l...

How do I copy the wording of a cell into a tab?
I have a large workbook with the tab titles available to copy to save typing every single one, is it possible to populate the tabs by a form of copy-paste You want your sheet tab name to reflect the value of a particular cell within that sheet? You want to copy a sheet and rename the copy to the value of a cell within that sheet? Please explain in more detail. The lack of sentence structure makes it difficult to get a clear picture. Gord Dibben MS Excel MVP On Sun, 24 Jan 2010 11:18:01 -0800, Jamie <Jamie@discussions.microsoft.com> wrote: >I have a large wo...

Word 2007 Background Color PDF
I was finally able to get the background color to print. I'm using a 2-color fill, fading from one color to the other. When I do it, it doesn't fade an even color top to bottom. It goes dark to light then starts toward light agaoin before it cuts off. (I have the correct box selected). Then, when I save as a PDF, it gives me 5 sets of the faded lines. That is, light to dark to light to dark... etc. 5 times. How can I set that "scale" or get it corrected? Thanx JDC Page backgrounds are more concerned with web page creation, which does not support pages...