#1
|
|||
|
|||
Macro to identify and fix long sentences without commas
I would like to find (or make) a macro that will identify (highlight) long run-on sentences that haven't been broken up with commas. Let's say 25+ words in a row without any commas. I have found a macro that will do something similar- it will find long sentences, but it will only identify long stretches between periods. I want to find the run on sentences where the author (not me, but text that I copy/paste into word) failed to break of the sentence into bite sized pieces by using commas. For instance:
He went to the bank and withdrew two hundred dollars before going to the store and buying a turkey for Thanksgiving dinner at his grandparents house in Tallahassee Florida on Thanksgiving day in 2019. Here is the macro I found for long sentences. Is there a way to modify it to do what I want it to do? Code:
Sub Mark_LongSentences() Dim iMyCount As Integer Dim iWords As Integer Dim mySent If Not ActiveDocument.Saved Then ActiveDocument.Save End If 'Reset counter iMyCount = 0 'Set number of words iWords = 30 For Each mySent In ActiveDocument.Sentences If mySent.Words.Count > iWords Then mySent.Font.Color = wdColorRed iMyCount = iMyCount + 1 End If Next MsgBox iMyCount & " sentences longer than " & _ iWords & " words." End Sub Last edited by Charles Kenyon; 03-24-2023 at 11:25 AM. Reason: some formatting on macro |
#2
|
|||
|
|||
I had hoped that the Grammar checking would have handled this, but it does not hiccup at your sample sentence, at least not about punctuation or run-on status. It does give it a Fletch-Kincaid grade level of 17, though (i.e. grad school).
You want the macro to break it up into logical portions and decide how to punctuate it? I'm afraid you are expecting too much from vba. Take a look at these discussions on a macro for proper title case to get an idea of how complex ordinary English can be.
Microsoft's new AI may handle this but do not hold your breath. I do not know that it will apply to Office 2021 anyway since it is labeled as a 365 product. Better would be if they get the grammar checker to make suggestions. You can use the Feedback mechanism in Word to suggest this, but again, do not hold your breath. |
#3
|
|||
|
|||
macro to identify long stretches of sentence without commas or periods
I am new to the forum, so I hope I am replying to the thread correctly.
My macro request is much simpler than that. I don't want it to make any grammar suggestions at all. I just want it to identify long stretches without commas or periods by highlighting them. The macro that I found can do the same thing, but only accounts for long sentences. I assume that is because it counts the number of words between periods. I want one that will count the number of words between either commas or periods. My use for this is in prepping manuscripts for narration. If I can identify a long, run-on sentence ahead of time, I can look at it and figure out ahead of time where to breathe. I can then add a comma or another indicator manually to tell myself to breathe when I am recording the narration. |
#4
|
|||
|
|||
(Added prior to above being posted)
Here is a macro to select the first long sentence found and suggest that it be changed. Note, even with commas added, it would trigger the suggestion. Code:
Sub LongSentencesQuery() ' Charles Kenyon ' points out long sentences, asks for changes ' https://www.msofficeforums.com/174325-post3.html ' Dim iMyCount As Long Dim i As Long Const iWords As Long = 30 Dim rSent As range ' Save active document before making changes If Not ActiveDocument.Saved Then ActiveDocument.Save End If 'Reset counter iMyCount = 0 'Set number of words Let iMyCount = ActiveDocument.Sentences.Count For i = 1 To iMyCount Set rSent = ActiveDocument.Sentences(i) If rSent.Words.Count > iWords Then MsgBox "The sentence: " & rSent.Text & " is long, consider breaking it up into smaller sentences." rSent.Select Exit Sub End If Next i Set rSent = Nothing End Sub It selects the problem sentence but does not mark it. |
#5
|
|||
|
|||
I've moved your new post to be in the same vba thread.
|
#6
|
|||
|
|||
Quote:
|
#7
|
|||
|
|||
To make it simple, here is a demo of the macro I currently have, and a demo of what I want to do. There are 2 problems with the macro that I have:
1. It identifies too many sentences (see demo) 2. It identifies sentences with 30+ words (I want it triggered at 25+ words) Example sentence without using macro: Sam woke up Thursday. He went to the bank and withdrew two hundred dollars before going to the store and buying a turkey for Thanksgiving dinner at his grandparents house in Tallahassee Florida on Thanksgiving day in 2019. He was nervous about going, because he hadn't seen his grandparents in a while, and they had a big, scary, mean dog, that was known to bite. Example sentence using "Mark_LongSentences" macro: (this macro gets triggered at 30+ word sentences but I only need it to be at 25+ word sentences. Sam woke up Thursday. He went to the bank and withdrew two hundred dollars before going to the store and buying a turkey for Thanksgiving dinner at his grandparents house in Tallahassee Florida on Thanksgiving day in 2019. Finally the time had come. Sam was nervous about going, because he hadn't seen his grandparents in a while, and they had a big, scary, mean dog, that was known to bite, and might even have rabies. Notice that it marked both long sentences, including the one with many commas. I don't want that. I only want it to mark the one that has a stretch of 25+ words without commas. Example sentence using my fantasy macro that does what I want it to do: Sam woke up Thursday. He went to the bank and withdrew two hundred dollars before going to the store and buying a turkey for Thanksgiving dinner at his grandparents house in Tallahassee Florida on Thanksgiving day in 2019. Finally the time had come. Sam was nervous about going, because he hadn't seen his grandparents in a while, and they had a big, scary, mean dog, that was known to bite, and might even have rabies. Notice how it only identified the first long sentence. Second one is fine because the 25+ word stretch is broken up into phrases with commas. |
#8
|
|||
|
|||
You are going to have an endless struggle developing a macro to achieve your desired result. While you example is clear enough, the problem is that Word has no fundamental concept of what a sentence is. To Word, all periods in text constitutes a sentence. So if your example looked like:
Mr. Sam went to the St. Charles St. Bank and withdrew two hundred dollars from Mr. Smith’s account before going to Dr. Harry’s Turkey Shop on N. Cherry St. to get a turkey on his way to his grandparent’s house at 9 Miller Ave. Tallahassee FL for dinner. To you an I, that looks like one very long sentence. To Word, it is 9 separate sentences. I have a fairly lengthy explanation of all this as well as an AddIn for evaluating deduced sentences: Deduced Sentences It may help you identify your sentences but nothing will be perfect. |
#9
|
|||
|
|||
... but here is a crude attempt to at least process your example text:
Code:
Sub ScratchMacro() 'A basic Word Macro coded by Gregory K. Maxey Dim oWord As Range Dim oRngFlag As Range Dim lngCount As Long Set oWord = ActiveDocument.Words(1) Set oRngFlag = oWord.Duplicate Do Debug.Print oWord.Characters(1) Select Case True Case oWord.Characters(1).Text Like "[A-Za-z]" lngCount = lngCount + 1 Case oWord.Characters(1).Text Like "[.,:;\!\?]" lngCount = 0 oWord.Select oRngFlag.Start = oWord.End End Select oWord.Move wdWord, 1 oRngFlag.End = oWord.End If lngCount = 30 Then Do oRngFlag.MoveEnd wdCharacter, 1 Loop Until oRngFlag.Characters.Last Like "[.,:;]" oRngFlag.HighlightColorIndex = wdBrightGreen oRngFlag.Collapse wdCollapseEnd lngCount = 0 End If Loop Until oWord.End = ActiveDocument.Range.End - 1 lbl_Exit: Exit Sub End Sub |
#10
|
|||
|
|||
Hey Greg
Thanks for the "crude attempt". I tried it but I got an error message. (Compile error: Expected end sub) Also, I fully expect that the macro will not catch many of the sentences (like ones with Dr. Smith, Mrs. Jones, etc. ) but as I normally do this whole process manually, it will still save me a lot of highlighting overall. |
#11
|
|||
|
|||
There is no unexpected "End Sub" in the code I posted. It is a complete procedure. You may have copied it incorrectly into your project.
|
#12
|
|||
|
|||
Ah, that was the issue. (copy/paste problem).
I tried it and it worked! It identified some long sentences in a sample text I put in. I am really excited about trying this on my next real project. Thanks so much! |
#13
|
|||
|
|||
You are welcome. Here is a little more polished version:
Code:
Sub ScratchMacro() 'A basic Word Macro coded by Gregory K. Maxey Dim oWord As Range Dim oRngFlag As Range Dim lngCount As Long Dim lngLimit As Long lngLimit = InputBox("Enter the long sentence word count", "LIMIT", "30") Set oWord = ActiveDocument.Words(1) Set oRngFlag = oWord.Duplicate Do Debug.Print oWord.Characters(1) Select Case True Case oWord.Characters(1).Text Like "[A-Za-z]" lngCount = lngCount + 1 Case oWord.Characters(1).Text Like "[.,:;\!\?]" lngCount = 0 oWord.Select oRngFlag.Start = oWord.End End Select oWord.Move wdWord, 1 oRngFlag.End = oWord.End If lngCount = lngLimit Then Do While Not oRngFlag.Characters.First Like "[A-Za-z]" oRngFlag.MoveStart wdCharacter, 1 Loop Do oRngFlag.MoveEnd wdCharacter, 1 Loop Until oRngFlag.Characters.Last Like "[.,:;\!\?" & Chr(13) & "]" oRngFlag.HighlightColorIndex = wdBrightGreen oRngFlag.Collapse wdCollapseEnd lngCount = 0 End If Loop Until oWord.End = ActiveDocument.Range.End - 1 lbl_Exit: Exit Sub End Sub |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to select multiple sentences that contain a specific word | MauiTruss | Word VBA | 7 | 10-03-2018 03:46 PM |
Macro to number sentences | tjf816 | Word VBA | 10 | 03-29-2017 05:42 PM |
A Widlcard or Macro to Properly Remove Spaces Between Commas? | CrossReach | Word | 6 | 01-23-2017 04:28 AM |
Need a Macro that Combines Every 5 sentences into a paragraph | jgarland | Word | 22 | 01-11-2012 11:19 AM |