Making Word skip one-sentence paragraphs in a search and replace

J

Jake

I'm using a range in the following code to highlight the first sentence of
each paragraph. Since the first sentence in a one-sentence paragraph (such as
each item in a list) is the entire paragraph, this code of course also
highlights every one sentence paragraph. Is there a way to use the count
parameter to limit the search and replace to only paragraphs that have more
than one sentence?

The current code:

Private Sub CommandButton37_Click()
'Highlight first sentence of each para.

Dim oPar As Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Range.Paragraphs
oPar.Range.Sentences.First.HighlightColorIndex = wdWhite
Next oPar

End Sub
 
S

StevenM

Perhaps something like:

Private Sub CommandButton37_Click()
'Highlight first sentence of each para.
Dim i As Long
Dim oPar As Range
For i = 1 To ActiveDocument.Paragraphs.count
Set oPar = ActiveDocument.Paragraphs(i).Range
If oPar.Sentences.count > 1 Then
oPar.Sentences.First.HighlightColorIndex = wdYellow
End If
Next i
End Sub
 
J

Jake

Your code does just what I needed. I was thinking that an "except" was
needed. Your if-then sequence works perfectly. Many thanks.
 

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