• Word macro to count the number of sentences with tracked changes

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Word macro to count the number of sentences with tracked changes

    Author
    Topic
    #508229

    Hi everyone,

    I’m looking for a Word macro that will count both the total number of sentences in a document and the number of sentences that contain tracked changes, and display the information in a message box (such as Total sentences: 250 Revised sentences: 189). It would need to work for both .doc and .docx files.

    Does anyone have any ideas? Many many thanks in advance.

    Viewing 2 reply threads
    Author
    Replies
    • #1591546

      Maybe something like

      Code:
      Sub CountSentences()
      Dim oRev As Revision
      Dim oSentence As Range
      Dim iSentenceCount As Integer: iSentenceCount = 0
      Dim iRevCount As Integer: iRevCount = 0
      Dim oStory As Range
          For Each oStory In ActiveDocument.StoryRanges
              For Each oSentence In oStory.Sentences
                  iSentenceCount = iSentenceCount + 1
                  If oSentence.Revisions.Count > 0 Then
                      iRevCount = iRevCount + 1
                  End If
                  DoEvents
              Next oSentence
              If oStory.StoryType  wdMainTextStory Then
                  While Not (oStory.NextStoryRange Is Nothing)
                      Set oStory = oStory.NextStoryRange
                      For Each oSentence In oStory.Sentences
                          iSentenceCount = iSentenceCount + 1
                          If oSentence.Revisions.Count > 0 Then
                              iRevCount = iRevCount + 1
                          End If
                          DoEvents
                      Next oSentence
                  Wend
              End If
          Next oStory
          MsgBox “Total sentences: ” & iSentenceCount & ” Revised sentences: ” & iRevCount
      lbl_Exit:
          Set oStory = Nothing
          Set oSentence = Nothing
          Exit Sub
      End Sub
      • #1591547

        Hi Graham,

        Excellent! Thank you very much indeed – works a treat!

        Now that I think about it… I wonder if it would be possible to also do the following:

        – of the words in the Revised sentences, count and display (at the bottom of the message box) the total number of newly added words (e.g. Revised words: 3140).
        – for these words, multiple changes to a word (e.g. removing a letter, adding an apostrophe, making it italic) would still count as one word, not three.

        Again, many thanks in advance.

    • #1591560

      Do be aware that VBA has no idea what a grammatical sentence is. For example, consider the following:
      Mr. Smith spent $1,234.56 at Dr. John’s Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green’s Mt. Pleasant macadamia nuts.
      For you and me, that would count as one sentence; for VBA it counts as five. With Graham’s code it reports nine!** Accordingly, the sentence count returned by Graham’s is not reliable. Furthermore, using ‘Track Changes’, if the above grammatical sentence is changed to:
      Mrs. Jones spent $1,234.56 at Dr. John’s Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Bob’s Mt. Pleasant peanuts.
      Graham’s code erroneously reports eight sentences of which four have changes, whereas there is only one (grammatical) sentence that has been changed.

      Long story short: the Sentence object in VBA cannot be used to generate the kind of report you want.

      ** That’s because VBA counts empty paragraphs/ranges as sentences and Graham’s code loops through ranges that may well be empty.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1591609

      Thanks Paul – hmm, yes I see what you mean!

    Viewing 2 reply threads
    Reply To: Word macro to count the number of sentences with tracked changes

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: