Consulting

Results 1 to 13 of 13

Thread: Macro for word to count Track Changes (Sub GetTCStats()) leads to errors

  1. #1

    Macro for word to count Track Changes (Sub GetTCStats()) leads to errors

    Hello,
    I need help in VB since I am not a coder, please bare with me if I do not describe well the issue.

    Translation professionals use Sub GetTCStats() Macro for Word in order to count insertions and deletions made in a Word document with Track Changes.

    In some documents, it does not work at all, and yields "Requested object is not available" (Run-time error '5852'). When I open the debugging dialog, it highlights the line "Select Case oRevision.Type"

    In other documents I tested, I do not receive this error, or any other, and it does appear "to work", but when I "step into" and hover this specific line ("Select Case oRevision.Type") I see it has a "oRevision.Type = <Object variable or With block variable not set>" comment in the tooltip.

    how can we update this to work in all cases?

    Very grateful to you all, ariellata


    Macro:

    Sub GetTCStats()
    Dim lInsertsWords As Long
    Dim lInsertsChar As Long
    Dim lDeletesWords As Long
    Dim lDeletesChar As Long
    Dim sTemp As String
    Dim oRevision As Revision
    lInsertsWords = 0
    lInsertsChar = 0
    lDeletesWords = 0
    lDeletesChar = 0
    For Each oRevision In ActiveDocument.Revisions
    Select Case oRevision.Type
    Case wdRevisionInsert
    lInsertsChar = lInsertsChar + Len(oRevision.Range.Text)
    lInsertsWords = lInsertsWords + oRevision.Range.Words.Count
    Case wdRevisionDelete
    lDeletesChar = lDeletesChar + Len(oRevision.Range.Text)
    lDeletesWords = lDeletesWords + oRevision.Range.Words.Count
    End Select
    Next oRevision
    sTemp = "Insertions" & vbCrLf
    sTemp = sTemp & " Words: " & lInsertsWords & vbCrLf
    sTemp = sTemp & " Characters: " & lInsertsChar & vbCrLf
    sTemp = sTemp & "Deletions" & vbCrLf
    sTemp = sTemp & " Words: " & lDeletesWords & vbCrLf
    sTemp = sTemp & " Characters: " & lDeletesChar & vbCrLf
    MsgBox sTemp
    End Sub
    Last edited by Paul_Hossler; 12-23-2019 at 12:46 PM. Reason: Added CODE Tags

  2. #2
    Without an example document that fails, it is difficult to determine why you get the error, but if you add an error trap before the 'For Each' line i.e.
    On Error Resume Next
    the error should be suppressed. An example document, would help determine a more robust error trap.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3

    thank you, but cannot expose the document -_-

    Quote Originally Posted by gmayor View Post
    Without an example document that fails, it is difficult to determine why you get the error, but if you add an error trap before the 'For Each' line i.e.
    On Error Resume Next
    the error should be suppressed. An example document, would help determine a more robust error trap.

    Hi gmayor,
    thank you for your kind reply. the document that produced the error is under confidentiality, so I sadly cannot share it.
    since it does not happen a lot, I got no other example to share.
    I do know, however, just by googling, that there are many translators out there experiencing the same.

    To conclude - will that supress the error? - not sure I added the line in the right position in the code...

    Thank you so much!

    Sub GetTCStats()
    Dim lInsertsWords As Long
    Dim lInsertsChar As Long
    Dim lDeletesWords As Long
    Dim lDeletesChar As Long
    Dim sTemp As String
    Dim oRevision As Revision
    lInsertsWords = 0
    lInsertsChar = 0
    lDeletesWords = 0
    lDeletesChar = 0
    On Error Resume Next
    For Each oRevision In ActiveDocument.Revisions
    Select Case oRevision.Type
    Case wdRevisionInsert
    lInsertsChar = lInsertsChar + Len(oRevision.Range.Text)
    lInsertsWords = lInsertsWords + oRevision.Range.Words.Count
    Case wdRevisionDelete
    lDeletesChar = lDeletesChar + Len(oRevision.Range.Text)
    lDeletesWords = lDeletesWords + oRevision.Range.Words.Count
    End Select
    Next oRevision
    sTemp = "Insertions" & vbCrLf
    sTemp = sTemp & " Words: " & lInsertsWords & vbCrLf
    sTemp = sTemp & " Characters: " & lInsertsChar & vbCrLf
    sTemp = sTemp & "Deletions" & vbCrLf
    sTemp = sTemp & " Words: " & lDeletesWords & vbCrLf
    sTemp = sTemp & " Characters: " & lDeletesChar & vbCrLf
    MsgBox sTemp
    End Sub

  4. #4
    It should suppress the error, though the counts may no longer be correct - try the revised code on your errant document.

    I notice that you mentioned translators. If this is not an English language document, what is the language involved. Language issues are relevant, though I don't see any obvious language related issue.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Thanks gmayor,
    the code made my Word 'not responding' and I had to stop it from the task manager.
    the language is English, and it is "real" track changes, and not just "make believe" colored text.
    I wish I could send it. I'm sure you could figure this out.
    Is there anyway in which I can debug this to provide you with more information?
    thank you for trying to help. ariellata.

  6. #6
    Try the following which selects the corrections as it processes them. If it crashes the item that caused the error should be highlighted

    Sub GetTCStats()
    
    Dim lInsertsWords As Long
    Dim lInsertsChar As Long
    Dim lDeletesWords As Long
    Dim lDeletesChar As Long
    Dim lRev As Long
    Dim sTemp As String
    Dim oRevision As Revision
    Dim bTrack As Boolean
    
        On Error GoTo err_Handler
        bTrack = ActiveDocument.TrackRevisions
        ActiveDocument.TrackRevisions = False
        lInsertsWords = 0
        lInsertsChar = 0
        lDeletesWords = 0
        lDeletesChar = 0
        For lRev = ActiveDocument.Revisions.Count To 1 Step -1
            Set oRevision = ActiveDocument.Revisions(lRev)
            oRevision.Range.Select
            Select Case oRevision.Type
                Case wdRevisionInsert
                    lInsertsChar = lInsertsChar + Len(oRevision.Range.Text)
                    lInsertsWords = lInsertsWords + oRevision.Range.Words.Count
                Case wdRevisionDelete
                    lDeletesChar = lDeletesChar + Len(oRevision.Range.Text)
                    lDeletesWords = lDeletesWords + oRevision.Range.Words.Count
                Case Else
            End Select
            DoEvents
        Next lRev
        sTemp = "Insertions" & vbCrLf
        sTemp = sTemp & " Words: " & lInsertsWords & vbCrLf
        sTemp = sTemp & " Characters: " & lInsertsChar & vbCrLf
        sTemp = sTemp & "Deletions" & vbCrLf
        sTemp = sTemp & " Words: " & lDeletesWords & vbCrLf
        sTemp = sTemp & " Characters: " & lDeletesChar & vbCrLf
        MsgBox sTemp
    lbl_Exit:
        ActiveDocument.TrackRevisions = bTrack
        Set oRevision = Nothing
        Exit Sub
    err_Handler:
        Beep
        MsgBox "The process encountered an uncorrectable error"
        Err.Clear
        GoTo lbl_Exit
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    Hi again,
    and much thanks for the updated code.
    I received "The process encountered an uncorrectable error".
    What else can I do?
    ariellata

  8. #8
    Without a sample of the document that causes the error, it is difficult to guess what the problem is that is causing it.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    I appreciate your efforts, and I sadly will wait till the next faulty document-macro results.
    I do not think I may expose my client...
    thank you again, ariellata

  10. #10
    VBAX Newbie
    Joined
    Feb 2023
    Posts
    2
    Location
    Hello, I'm trying this very same macro on a particularly long document. I tried gmayor's macro and got the "The process encountered an uncorrectable error" window with a cross-reference highlighted in the document. I assume this was the original culprit, so I deleted it, but, after re-running the original macro, I still get the 5852 error, so I then to re-ran gmayor's macro and, again, got the "The process encountered an uncorrectable error", but this time no text is highlighted and nothing else happens.

    I'm not sure what else could be done at this point? The macro works perfectly in other documents, but not this particular one.

  11. #11
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    Quote Originally Posted by rhrs1987 View Post
    The macro works perfectly in other documents, but not this particular one.
    So, what is the differences between your current document and the others that the macro worked in?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  12. #12
    VBAX Newbie
    Joined
    Feb 2023
    Posts
    2
    Location
    Quote Originally Posted by Aussiebear View Post
    So, what is the differences between your current document and the others that the macro worked in?
    Hi, Aussiebear: the difference is my current document is quite big (74 pages long, contains text and table insertions and only 1 image insertion: the part I'm working on only contains text and a lot table insertions) and the other document is 27 pages (only text insertions). It also contains cross-references throughout the entire document. I don't know if cross-references can be a problem with macros (?). Also, I'm using Word 2016, Windows 10, if that helps.

    I appreciate the help!

  13. #13
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    Have you tried running the macro on smaller versions of your document, namely one that contains only text, another than contains text and table insertions, another that contains text and cross references, and finally one that contains text and an image? This way we can try to track down what causes the error.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •