Home > Enterprise >  How can you catch or test for VBA error 5825 in a Word 365 document?
How can you catch or test for VBA error 5825 in a Word 365 document?

Time:11-15

This macro (with minor changes, it likely originated on a Stack Exchange site) is fairly popular with authors that need to convert lots of tracked changes to regular text that has been colored blue:

Sub accept_changes()
    tempState = ActiveDocument.TrackRevisions

    ActiveDocument.TrackRevisions = False
    For Each Revision In ActiveDocument.Revisions
        Set Change = Revision.Range
        Change.Revisions.AcceptAll
        Change.Font.Color = 12611584
    Next
    
    ActiveDocument.TrackRevisions = tempState
End Sub

However, when using other macros or plug-ins such as enter image description here

Also, be careful when naming variables. For Each Revision In ActiveDocument.Revisions is bad practice as Revision is the name of an object in Word's object model.

CodePudding user response:

With a bit of hunting around and viewing of the field codes used by Word, it appears that this error was due to a corrupted field code. There's some evidence in the Microsoft support forum that this might be a known, but infrequent error that can be addressed by showing field codes.

Testing against my document that generated the error, the following macro runs correctly and did not generate any errors.

Option Explicit

Sub accept_changes()
    Dim tempState As Boolean, count As Integer, change As Range, thisRevision As Revision

    ' Check if there is work to do
    count = ActiveDocument.Revisions.count
    If count = 0 Then
        MsgBox "No revisions to process", vbInformation
        Exit Sub
    End If
    
    ' Disable screen updating for performance
    Application.ScreenUpdating = False

    With ActiveDocument
    
        ' Note the current state of revision tracking and disable them
        tempState = .TrackRevisions
        .TrackRevisions = False
                
        ' Show field codes incase any are corrupted
        .ActiveWindow.View.ShowFieldCodes = True
                
        ' Iterate through each revision, accept them and highlgiht the range in blue
        For Each thisRevision In .Revisions
            Set change = thisRevision.Range
            change.Revisions.AcceptAll
            change.Font.Color = 12611584
        Next
        
        ' Restore application state
        .ActiveWindow.View.ShowFieldCodes = False
        .TrackRevisions = tempState
    
    End With
    
    ' Reenable screen updating
    Application.ScreenUpdating = True
    
    ' Let the user know we are done
    MsgBox "Accpeted "   Str(count)   " revision(s)", vbInformation
    
End Sub
  • Related