none
help please... a Macro for Word that counts Track Changes (Sub GetTCStats()) yields errors :( RRS feed

  • Question

  • Hi Everyone, 

    I need help in VB since I am not a coder.

    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, and yields "Requested object is not available" (Run-time error '5852') and when I open debugging it highlights the line "Select Case oRevision.Type"

    In other documents I tested, I do not receive an error, but when I "step into" and hover this line, I see it has a
    "oRevision.Type = <Object variable or With block variable not set>".

    Is the object not set correctly? Can anyone help solve this issue? 

    very grateful for any help,

    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

    Thursday, December 19, 2019 12:23 AM

All replies

  • This is just a guess, but one possibility is that the oRevision itself has been deleted already.

    Sometimes in Word the solution to that is to iterate backwards through a collection, e.g. instead of a For Each, use 

    For i = ActiveDocument.Revisions.Count To 1 Step -1
      With ActiveDocument.Revisions(i)    
        ' do some stuff  
      End With
    Next

    But because Revisions are complicated, maybe that won't work either. So perhaps you could do something like

    For Each oRevision In ActiveDocument.Revisions
      If Not (oRevision Is Nothing)
        ' do some stuff
      End If
    Next
    


    Monday, December 30, 2019 9:15 PM