Consulting

Results 1 to 8 of 8

Thread: Insertion and deletion counter

  1. #1
    VBAX Regular
    Joined
    Oct 2016
    Posts
    45
    Location

    Insertion and deletion counter

    Hi VBA gang,

    A new week, a new headache. I absolutely cannot work out why I'm getting a "Run-time error '5852'. Requested object is not available" error on this one. Thought it was a simple code, but no! The debugger seems to flip out on the select case part of the code. Any ideas why this simple code isn't working? All I'm trying to do is count tracked insertions and deletions.

    Sub InDel_Counter()
    
    Dim rvcnt, rvcnt2, a As Long
    Dim oRevision As Revision
        
        rvcnt = ActiveDocument.Revisions.Count
        
        rvcnt2 = 0
        
    For Each oRevision In ActiveDocument.Revisions
        Select Case oRevision.Type
            Case wdRevisionInsert, wdRevisionDelete
                With oRevision
                    If .Type = wdRevisionInsert Or wdRevisionDelete Then rvcnt2 = rvcnt2 + 1
                End With
        End Select
    Next oRevision
            
    MsgBox "There are " & rvcnt2 & " tracked insertions and deletions.", vbOKOnly
        
    End Sub
    *Updated to add:*

    I've even tried a much more convoluted way, but to no avail:

    Sub InDel_Counter()
    
    Dim rvcnt, rvcnt2, a As Long
    Dim oRevision As Revision
    Dim tDoc As Document
    
    
    Set tDoc = ActiveDocument
        
        rvcnt = tDoc.Revisions.Count
        
        rvcnt2 = 0
        
    For Each oRevision In tDoc.Revisions
        If oRevision.Type = wdRevisionConflict Or wdNoRevision Or wdRevisionCellDeletion Or wdRevisionCellInsertion Or wdRevisionCellMerge _
            Or wdRevisionCellSplit Or wdRevisionConflict Or wdRevisionConflictDelete Or wdRevisionConflictInsert Or wdRevisionDisplayField _
            Or wdRevisionMovedFrom Or wdRevisionMovedTo Or wdRevisionParagraphNumber Or wdRevisionParagraphProperty Or wdRevisionProperty _
            Or wdRevisionReconcile Or wdRevisionReplace Or wdRevisionSectionProperty Or wdRevisionStyle Or wdRevisionStyleDefinition _
            Or wdRevisionTableProperty Then
                GoTo NextoRevision
        ElseIf oRevision.Type = wdRevisionDelete Or wdRevisionInsert Then
            rvcnt2 = rvcnt2 + 1
        Else
            GoTo NextoRevision
        End If
    NextoRevision:
    Next
            
    MsgBox "There are " & rvcnt2 & " tracked insertions and deletions.", vbOKOnly
        
    End Sub
    Last edited by h2whoa; 06-26-2018 at 10:23 AM. Reason: Additional detail

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Your code runs without error here.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Oct 2016
    Posts
    45
    Location
    Hmmmm, interesting. Could it be crashing if it's a massive document with lots of changes?

    Wonder if there's a way to break it up so it doesn't crash?

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Maybe you can figure out what the error is by trying to skip it.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Just code reading ...


    Sub InDel_Counter() Dim rvcnt As Long, rvcnt2 as Long, a As Long ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Dim oRevision As Revision rvcnt = ActiveDocument.Revisions.Count rvcnt2 = 0 For Each oRevision In ActiveDocument.Revisions Select Case oRevision.Type Case wdRevisionInsert, wdRevisionDelete rvcnt2 = rvcnt2 + 1 ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< End Select Next oRevision MsgBox "There are " & rvcnt2 & " tracked insertions and deletions.", vbOKOnly End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Or even:
    Sub InDel_Counter()
    Dim Rvn As Revision, lRvn As Long
    For Each Rvn In ActiveDocument.Revisions
      Select Case Rvn.Type
        Case wdRevisionInsert, wdRevisionDelete: lRvn = lRvn + 1
      End Select
    Next
    MsgBox "There are " & lRvn & " tracked insertions and deletions.", vbOKOnly
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    That is not the problem. I know there are documents (I've got some, I assume with revisions in a header, footer, footnote or somewhere else), where ActiveDocument.Revisions is a collection that may contain non-existing-elements. You can not find them with "if myRevision is nothing", and you can not access anything inside. The debugger says the item is deleted. And if those "deleted items" are there, they always are the last ones in the collection.

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    That suggests you've got a corrupt document.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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