PDA

View Full Version : Checking number of words in a sentence



mattvb
10-14-2016, 06:43 AM
Hi,

I appreciate that what I am asking may not be possible as it seems that VBA has a strange concept of 'sentence'.

I'm currently using the following macro to highlight any sentences that contain over 20 words:


Sub HighlightLongSentences()
Dim Count As Integer
Dim Words As Integer
If Not ActiveDocument.Saved Then
ActiveDocument.Save
End If


Count = 0


Words = 20


For Each Check In ActiveDocument.Sentences
If Check.Words.Count > Words Then
Check.HighlightColorIndex = wdYellow
Count = Count + 1
End If
Next
MsgBox "This document contains " & Count & " sentence/s that have more than " & _
Words & " words."
For Each Check In ActiveDocument.Sentences
If Check.Words.Count < Words Then
Check.HighlightColorIndex = wdNoHighlight
Count = Count + 1
End If
Next
End Sub




It works, however, it counts punctuation marks etc. as words. Which isn't very helpful!

I also came across this code, which seems to count things correctly:



Sub CountWords()
Application.ScreenUpdating = False
Dim oPara As Paragraph, lCpt As Long
With Selection


For Each oPara In .Paragraphs
If oPara.Style = "Caption" Then _
lCpt = lCpt + oPara.Range.ComputeStatistics(wdStatisticWords)
Next
MsgBox "Word Count Statistics:" & vbCr & _
"Other - " & vbTab & vbTab & .Range.ComputeStatistics(wdStatisticWords) - lCpt
End With
Application.ScreenUpdating = True
End Sub


Is there any way that I can combine the two? So I can use the accurate count of the second example to highlight the long sentences as shown in the first example?

Thanks for your time. As you can probably tell I'm pretty new to VBA, but after spending hours trying to figure this out I'm hoping someone can point me in the right direction.

gmaxey
10-14-2016, 10:40 AM
It isn't possible to be accurate because Word does't really know what a sentence is. This will get you closer:


Sub HighlightLongSentences()
Dim oSent As Range
Dim Count As Integer
Dim Words As Integer
Count = 0
Words = 20
For Each oSent In ActiveDocument.Sentences
If oSent.ComputeStatistics(wdStatisticWords) > Words Then
oSent.HighlightColorIndex = wdYellow
Count = Count + 1
End If
Next
MsgBox "This document contains " & Count & " sentence/s that have more than " & _
Words & " words."
End Sub

gmaxey
10-14-2016, 10:41 AM
P.S. Why do you declare some variables e.g., Count and Words but not others e.g., Check?

mattvb
10-14-2016, 06:00 PM
That is wonderful, that you for your assistance Greg.

Do you know what sentence structures trips up this code? In the tests I've done it seems to count things a lot more accurately than the code I was using.

As for why I declared some variables and not others... that's simply because I don't know what I'm doing!

gmaxey
10-14-2016, 06:33 PM
While perhaps poor grammar, I would consider this one long sentenced:

Counting his way up to twenty or more words, Mr. Magoo will always trip up on abbreviated words, like (Mr.) regardless if there are twenty words in the sentence or not.

Word sees it as three sentences.

mattvb
10-14-2016, 06:54 PM
Thanks for the example Greg. I really appreciate your help with this. All the best.

mattvb
10-19-2016, 04:57 AM
I tried to set this up as an add-in and I get a "Wrong number of arguments or invalid property assignment" error when it runs.

Any idea what could be causing that?

gmaxey
10-20-2016, 05:19 AM
What exactly did you do?

mattvb
10-20-2016, 01:55 PM
I created the add-in by following a tutorial provided to me in the PowerPoint forum (check my post history, for some reason I'm not able to share the link here). It works in so far as the new button appears in word, but when pressed, I get the error message I mentioned earlier.

Thanks for taking a look Greg.

gmaxey
10-20-2016, 04:55 PM
It sounds like you do not have the VBA callback for your ribbon OnAction macro defined properly. See my:
http://gregmaxey.mvps.org/word_tip_pages/customize_ribbon_main.html

mattvb
10-20-2016, 08:38 PM
Thanks Greg, I think you are right.

I was able to follow your tutorial and set it up via that example, but I got lost trying to customise it for my project.

My macro is:


Sub HighlightLongSentences()
Dim oSent As Range
Dim Count As Integer
Dim Words As Integer
Count = 0
Words = 20
For Each oSent In ActiveDocument.sentences
If oSent.ComputeStatistics(wdStatisticWords) > Words Then
oSent.HighlightColorIndex = wdYellow
Count = Count + 1
End If
Next
MsgBox "This document contains " & Count & " sentence/s that have more than " & _
Words & " words."
For Each oSent In ActiveDocument.sentences
If oSent.ComputeStatistics(wdStatisticWords) <= Words Then
oSent.HighlightColorIndex = wdNoHighlight
Count = Count + 1
End If
Next
End Sub


What VBA Callback would I need for this? Sorry for the 20 questions, but VBA is really new to me and I don't really understand how it works.

gmaxey
10-21-2016, 08:02 AM
What do you have defined as the OnAction macro in your ribbonXML? If OnAction="HighlightLongSentences" then at the very least you need:

Sub HighlightLongSentences(ByVal control As IRibbonControl)