Consulting

Results 1 to 6 of 6

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
    2,845
    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
    2,845
    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 Wizard
    Joined
    Apr 2007
    Posts
    5,906
    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

    ------------------------------------------------------------------------------------------------------------------------
    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)
    (multiple files can be selected while holding Ctrl key) / 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
    3,607
    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
    [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
  •