Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-24-2023, 10:14 AM
Bunnelope Bunnelope is offline Macro to identify and fix long sentences without commas Mac OS X Macro to identify and fix long sentences without commas Office 2021
Novice
Macro to identify and fix long sentences without commas
 
Join Date: Mar 2023
Posts: 5
Bunnelope is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 03-24-2023, 11:01 AM
Charles Kenyon Charles Kenyon is offline Macro to identify and fix long sentences without commas Windows 11 Macro to identify and fix long sentences without commas Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,081
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

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.
That is a simple task compared to what you are contemplating.

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.
Reply With Quote
  #3  
Old 03-24-2023, 11:25 AM
Bunnelope Bunnelope is offline Macro to identify and fix long sentences without commas Mac OS X Macro to identify and fix long sentences without commas Office 2021
Novice
Macro to identify and fix long sentences without commas
 
Join Date: Mar 2023
Posts: 5
Bunnelope is on a distinguished road
Default 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.
Reply With Quote
  #4  
Old 03-24-2023, 11:59 AM
Charles Kenyon Charles Kenyon is offline Macro to identify and fix long sentences without commas Windows 11 Macro to identify and fix long sentences without commas Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,081
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

(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
Note that this is never going to go past the first problem sentence until it is corrected. It does not suggest comma placement.
It selects the problem sentence but does not mark it.
Reply With Quote
  #5  
Old 03-24-2023, 12:35 PM
Charles Kenyon Charles Kenyon is offline Macro to identify and fix long sentences without commas Windows 11 Macro to identify and fix long sentences without commas Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,081
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

I've moved your new post to be in the same vba thread.
Reply With Quote
  #6  
Old 03-24-2023, 12:43 PM
Charles Kenyon Charles Kenyon is offline Macro to identify and fix long sentences without commas Windows 11 Macro to identify and fix long sentences without commas Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,081
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Quote:
Originally Posted by Bunnelope View Post
***
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.
If I am now reading you correctly, what you want is a macro that checks text and if there is a sentence that is longer than 30 words, you want to check for the presence of a comma and then check to see

  1. if there is a portion of that sentence between the start of the sentence and the first comma,
  2. or between that comma and the next,
  3. or between the last comma and the period, is more than ? how many? words, you want that sentence marked so you can review it.
This, vba can do, but you need to determine how many words are allowed in a phrase before a breath. What is too long for a sentence? For a phrase?
Reply With Quote
  #7  
Old 03-24-2023, 10:41 PM
Bunnelope Bunnelope is offline Macro to identify and fix long sentences without commas Mac OS X Macro to identify and fix long sentences without commas Office 2021
Novice
Macro to identify and fix long sentences without commas
 
Join Date: Mar 2023
Posts: 5
Bunnelope is on a distinguished road
Default

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.
Reply With Quote
  #8  
Old 03-25-2023, 06:44 AM
gmaxey gmaxey is offline Macro to identify and fix long sentences without commas Windows 10 Macro to identify and fix long sentences without commas Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 03-25-2023, 07:18 AM
gmaxey gmaxey is offline Macro to identify and fix long sentences without commas Windows 10 Macro to identify and fix long sentences without commas Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

... 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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 03-29-2023, 01:03 PM
Bunnelope Bunnelope is offline Macro to identify and fix long sentences without commas Mac OS X Macro to identify and fix long sentences without commas Office 2021
Novice
Macro to identify and fix long sentences without commas
 
Join Date: Mar 2023
Posts: 5
Bunnelope is on a distinguished road
Default

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.
Reply With Quote
  #11  
Old 03-29-2023, 01:10 PM
gmaxey gmaxey is offline Macro to identify and fix long sentences without commas Windows 10 Macro to identify and fix long sentences without commas Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #12  
Old 03-29-2023, 01:19 PM
Bunnelope Bunnelope is offline Macro to identify and fix long sentences without commas Mac OS X Macro to identify and fix long sentences without commas Office 2021
Novice
Macro to identify and fix long sentences without commas
 
Join Date: Mar 2023
Posts: 5
Bunnelope is on a distinguished road
Default

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!
Reply With Quote
  #13  
Old 03-29-2023, 01:33 PM
gmaxey gmaxey is offline Macro to identify and fix long sentences without commas Windows 10 Macro to identify and fix long sentences without commas Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to identify and fix long sentences without commas Macro to select multiple sentences that contain a specific word MauiTruss Word VBA 7 10-03-2018 03:46 PM
Macro to identify and fix long sentences without commas 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
Macro to identify and fix long sentences without commas Need a Macro that Combines Every 5 sentences into a paragraph jgarland Word 22 01-11-2012 11:19 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:11 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft