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:
Code:
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
thank you, but cannot expose the document -_-
Quote:
Originally Posted by
gmayor
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.
Code:
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!
Code:
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