iterating over sentences in a given paragraph

B

BHW

I have a little macro that finds long sentences in my entire document
and highlights the first word of each long sentence. Generally I will
fix the offending sentence, but it still might be too long. I'd like a
macro that checks each sentence in the paragraph containing the
offending sentence and removes highlighting (if necessary). Here's
what I have so far.

For the entire document:

Sub longsent()
Dim item As Range

For Each item In ActiveDocument.Sentences

'item.Select

If item.Words.Count > 40 Then
item.Words.First.HighlightColorIndex = wdYellow
'MsgBox ("Num words = " & item.Words.Count)
End If


Next item
End Sub

this works for individual sentences, where I have the cursor inside of
the sentence.

Sub testsent()
Selection.Range.Sentences.First.Select
Selection.Words.First.HighlightColorIndex = wdNoHighlight
If Selection.Words.Count > 40 Then
MsgBox ("still long " & Selection.Words.Count & " words")
Selection.Words.First.HighlightColorIndex = wdYellow
End If
Selection.Range.Sentences.First.Select
rem try to deselect sentence - doesn't work


End Sub

Thanks!
 
D

Doug Robbins - Word MVP

The following displays the first word of each sentence in the paragraph in
which the selection is located if the sentence contains more than 40 words.
You can adapt it to do what you want.

Dim asentence As Range
For Each asentence In Selection.Paragraphs(1).Range.Sentences
If asentence.Words.Count > 40 Then
MsgBox asentence.Words(1)
End If
Next


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

BHW

That's great (thank you). If you could show me the additional code for
running this code over the entire document, I'd be all set. thanks
again.

Bruce

PS Any suggestions for where to learn more about programming Word?
 
D

Doug Robbins - Word MVP

If you had asked for that, I would have given it to you. It is simply

Dim asentence As Range
For Each asentence In ActiveDocument.Range.Sentences
If asentence.Words.Count > 40 Then
MsgBox asentence.Words(1)
End If
Next


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

BHW

Perfect! Thanks.

Bruce

If you had asked for that, I would have given it to you. It is simply

Dim asentence As Range
For Each asentence In ActiveDocument.Range.Sentences
If asentence.Words.Count > 40 Then
MsgBox asentence.Words(1)
End If
Next

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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