Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

iterating over sentences in a given paragraph

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BHW - 20 Mar 2008 23:46 GMT
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!
Doug Robbins - Word MVP - 22 Mar 2008 23:19 GMT
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

Signature

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

>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
[quoted text clipped - 36 lines]
>
> Thanks!
BHW - 24 Mar 2008 19:13 GMT
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?

On Mar 22, 6:19 pm, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> 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.
[quoted text clipped - 55 lines]
>
> > Thanks!
Doug Robbins - Word MVP - 24 Mar 2008 20:58 GMT
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

Signature

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

> 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
[quoted text clipped - 67 lines]
>>
>> > Thanks!
BHW - 24 Mar 2008 22:42 GMT
Perfect! Thanks.

Bruce

On Mar 24, 3:58 pm, "Doug Robbins - Word MVP"
<d...@REMOVECAPSmvps.org> wrote:
> If you had asked for that, I would have given it to you.  It is simply
>
[quoted text clipped - 88 lines]
>
> >> > Thanks!

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.