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.
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.