PDA

View Full Version : Automatic character count within each paragraph



jomama
11-12-2012, 05:00 AM
I am working in Word 2007/10.

In any given document, I would like to find each paragraph that falls outside of a certain character range.

The acceptable paragraph range for this type of project is 235-255 characters long (including spaces). Specifically I would like to be alerted if a paragraph falls between 50-234 characters or is 256 characters or more.

Ideally, the paragraph that is between 50-234 characters (including spaces) would be selected by a comment box that says, "Under character count." Whereas a paragraph that is 256 characters or more (including spaces), would be selected by a comment box that says "Over character count."

If that's not possible, then I think that just a simple highlight of the unacceptably-lengthed paragraph would be sufficient--unacceptable being 50-234 or 256+. Unless you could think of something better.

(Alternatively, if anyone knows a Wildcard way to find this range, that would work too.)

Thanks for your help!

gmaxey
11-12-2012, 08:40 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPar As Word.Paragraph
Dim oComment As Comment
Dim lngIndex As Long
For lngIndex = ActiveDocument.Comments.Count To 1 Step -1
If InStr(ActiveDocument.Comments(lngIndex).Range.Text, "CC") = 1 Then
ActiveDocument.Comments(lngIndex).Delete
End If
Next lngIndex
For Each oPar In ActiveDocument.Range.Paragraphs
Select Case oPar.Range.Characters.Count
Case Is > 256
ActiveDocument.Comments.Add oPar.Range, "CC - Over character count."
Case 50 To 234
ActiveDocument.Comments.Add oPar.Range, "CC - Under character count."
End Select
Next oPar
lbl_Exit:
Exit Sub
End Sub

jomama
11-12-2012, 09:54 AM
Wow!! That's awesome!! Thanks so much ...that is exactly what I was trying to get. Wow!! I have a lot to learn! Thank you so much!

jomama
11-13-2012, 12:52 PM
Is it possible to show the exact character count of the paragraph in each comment as well?

Currently the comment says, for example, "Under Character Count" (and that has been quite helpful, so thank you again!). But could the comment also tell me the character count? For example, if the paragraph were 219 characters long (including the spaces), could the comment say: "Under Character Count --219 characters"?

Thanks again!

gmaxey
11-13-2012, 12:54 PM
I've tried to modify the code here. See changes for first Case.


Sub ScratchMacro()

'A basic Word macro coded by Greg Maxey
Dim oPar As Word.Paragraph
Dim oComment As Comment
Dim lngIndex As Long
For lngIndex = ActiveDocument.Comments.Count To 1 Step -1
If InStr(ActiveDocument.Comments(lngIndex).Range.Text, "CC") = 1 Then
ActiveDocument.Comments(lngIndex).Delete
End If
Next lngIndex
For Each oPar In ActiveDocument.Range.Paragraphs
Select Case oPar.Range.Characters.Count
Case Is > 256 ActiveDocument.Comments.Add oPar.Range, "CC - Over character count - " & oPar.Range.Characters.Count & " characters."
Case 50 To 234 ActiveDocument.Comments.Add oPar.Range, "CC - Under character count."
End Select
Next oPar
lbl_Exit:
Exit Sub
End Sub

jomama
11-13-2012, 03:01 PM
Gotcha. I changed the Under-Character-Count case and indented it according to the layout of your first macro, and it works great! Thanks again for your help!!