none
5852 Error iterating through Revisions in Word 2010 RRS feed

  • Question

  • Hello,

    I have been trying to write a simple Macro to iterate through all of the revisions and output these to Excel for the benefit of some of my friends at work. 

    My macro works perfectly most of the time but in one document it throws a 5852 error. 

    I have read quite a bit about this and it seems there is a bug in Word which throws this error when iterating through revisions using a For Each loop or where there have been multiple revisions in the same table:

    http://support.microsoft.com/kb/913804?wa=wsignin1.0 

    I have looked at this fix and adding to Office 14.0 -> Word -> Options does not resolve my issue.

    I have also tried a whole load of work arounds:

    - Ignoring the 5852 error

    - iterating through revisions in each paragraph and then moving to the next paragraph when I hit this error

    - iterating through revisions in each paragraph and then moving to the next paragraph immediately if it is in a table - therefore ignoring paragraphs in tables

    - I have tried using a For Next loop to iterate through revisions - as per MS documentation

    Outcome:

    - For Next loops are not performant.  It was so slow I didn't even wait until I hit the problem area to find out if it would work through the erroneous part of the document - it took over an hour for 100 pages - who is going to use that macro?

    -  Any of the above workarounds just put my document into an infinite loop.

    I have scoured the web and cannot fix this issue.  It seems to have been known about since Word 2003 but I haven't seen one clean fix - even the one for Word 2003 says that this problem can still occur even after the fix.  You would think that outputting something as simple as track changes through VBA would be a relatively straight forward process!

    Can anyone help?

    Mike

    Monday, March 11, 2013 4:18 AM

All replies

  • Any luck on this one?

    I'm experience the very same thing using a simple macro that loops though all revisions and accepts deletions only. It's worked on other documents before but I can see in this particular document that it trips up on one particular revision and then it's game over from there...

    Friday, August 9, 2019 11:20 AM
  • As you have not posted your code or linked to the document in order to establish what causes the error (and the link in the original post in the thread is no longer available) it is difficult to comment, however if you are removing stuff that involves a count you should do so from the end and search backwards or the count will go to pieces e.g.

    Sub Macro1()

    'Graham Mayor - https://www.gmayor.com - Last updated - 10 Aug 2019' Dim oRev As Revision Dim bRev As Boolean Dim i As Long bRev = ActiveDocument.TrackRevisions ActiveDocument.TrackRevisions = False For i = ActiveDocument.Revisions.Count To 1 Step -1 Set oRev = ActiveDocument.Revisions(i) If oRev.Type = wdRevisionDelete Then oRev.Accept End If Next i ActiveDocument.TrackRevisions = bRev End Sub



    Graham Mayor - Word MVP
    www.gmayor.com


    Saturday, August 10, 2019 6:25 AM
  • Thanks, I'm in the processing of trying this, but of interest why are you switching 'track changes' Off when running through the code?
    Monday, August 12, 2019 4:13 PM
  • Even in reverse I'm getting the same issues. Here's the code that is derived from here:

    https://stackoverflow.com/questions/52809603/vba-word-conditionally-accept-changes-in-headers-and-footers

    Public Sub Document_AcceptDeletions()

    'Public Declarations
        Dim Sctn As Section
        Dim NewRevision As Revision
        Dim StorySect As Word.Range
        Dim HdFt As HeaderFooter

        On Error GoTo RevErr

        For Each StorySect In ActiveDocument.StoryRanges
            'Debug.Print StorySect.StoryType
            For Each NewRevision In StorySect.Revisions
                Select Case NewRevision.Type
                    Case Is = 2    '1: wdRevisionInsert  2: wdRevisionDelete  9: wdRevisionReplace
                        NewRevision.Accept

                    Case Else

                End Select

            Next NewRevision
            Do While Not (StorySect.NextStoryRange Is Nothing)
                Set StorySect = StorySect.NextStoryRange

                For Each NewRevision In StorySect.Revisions
                    Select Case NewRevision.Type
                        Case Is = 2    '1: wdRevisionInsert  2: wdRevisionDelete  9: wdRevisionReplace
                            NewRevision.Accept

                        Case Else

                    End Select

                Next NewRevision
            Loop
            Next StorySect '<<

    lbl_Exit:
            Exit Sub
    RevErr:
            If Err.Number <> 5852 Then
                Err.Clear
                GoTo lbl_Exit
            Else
                Err.Clear
                Resume
            End If
    End Sub

    The current document only contains Insertions (i.e. NewRevision.Type = 1) but at some point in the loop I get:

    NewRevision.Type = <Requested Object is not available>


    Tuesday, August 13, 2019 8:24 AM
  • I'm also experiencing the exact same problem.

    After countless hours, the issue comes down to tables.

    If there is a table with a revision in it, the code for iterating through the revisions just breaks.

    I can't find a way around it, this also includes TablesOfContents.

    I have no idea why the .Revisions is breaking on these tables.

    I also experienced an infinite loop if I try to ignore the error.

    So I looked around and found this from another site:

    "If you create a Microsoft Visual Basic for Applications (VBA) macro that loops through the Revisions collection by using a For Each loop on a Revision collection object, you may experience the following symptoms:
    * The loop stops responding when it tries to process some revisions in a Word 2002 document.
    * The macro appears to enter an infinite loop.

    I've expanded this with a post on stackoverfow too https://stackoverflow.com/questions/57483105/error-when-iterating-through-a-word-documents-revisions-when-the-docx-contains




    • Edited by crustyquakes Tuesday, August 13, 2019 8:08 PM added info
    Tuesday, August 13, 2019 3:08 PM