Consulting

Results 1 to 6 of 6

Thread: How to know if there are any more comments or revisions in a Word document?

  1. #1

    How to know if there are any more comments or revisions in a Word document?

    I am using the statement WordBasic.NextChangeOrComment in a macro that moves to the next comment or revision in a document (and then finds the right place in the original, unrevised document). It's all working nicely, but if there are no more revisions or comments in the document, Word displays the message "Do you want to continue searching from the beginning of the document?" If the user replies OK, the search begins all over from the top of the document.

    This is not always what I would like to happen. Is there a way of checking programmatically whether there are any more revisions or comments in a document before executing the statement WordBasic.NextChangeOrComment?

    Basically, Word is trying to take charge, which is in conflict with what the macro is trying to do!

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    I don't understand what you want to do.

    Sub test()
    
    
        If ActiveDocument.Revisions.Count + ActiveDocument.Comments.Count > 0 Then
            On Error Resume Next
            WordBasic.NextChangeOrComment
            If Err.Number <> 0 Then
                Err.Clear
                Exit Sub
             End If
            MsgBox Selection.Text
        End If
            
    End Sub

  3. #3
    Thanks. That looks like it should do the trick. It was the Err.Number stuff I didn't know about. I have to go out right now, but will check it later and report back!

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    mana,

    I with you. I don't understand what John is trying to do either but it seems all the error handler will do is suppress the run time error that occurs if you press "No" to the that dialog question. It doesn't stop the dialog from popping up.

    To do that, you need to evaluate the number of revisions and comments from the current select to the end of the document. Something like this:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 7/29/2017
    Dim oRng As Range
      Set oRng = Selection.Range
      oRng.End = ActiveDocument.Range.End
      If oRng.Revisions.Count + oRng.Comments.Count > 0 Then
        WordBasic.NextChangeOrComment
          Select Case Selection.StoryType
            Case Is = 4
              MsgBox Selection.Comments(1).Range.Text
              Set oRng = Selection.Comments(1).Reference
              oRng.Select
              Selection.Collapse wdCollapseEnd
            Case 1
              MsgBox Selection.Text
              oRng.Revisions(1).Range.Select
              Selection.Collapse wdCollapseEnd
          End Select
      End If
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Thanks!
    I always learn from your codes.

  6. #6
    Yes, me too! Looking at Greg's code led to the realization that all I needed was a function that told me whether there were any further revisions or comments in the document, so that I could handle the return to the top of the document if the user replied OK to the dialogue. A little complex to explain why, but it is to do with VBA's placing the text cursor in the comment itself, when I actually want the text cursor to end up in the main text story immediately before the comment. The function is below. Job done! Nothing like the help of someone who knows VBA thoroughy! Thanks.

    Function lastCommentOrRevision() As Boolean
         'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 7/29/2017
         ' Returns True if there are no further revisions or comments in the document
        Dim oRng As Range
        
        Set oRng = Selection.Range
        oRng.End = ActiveDocument.Range.End
        If oRng.Revisions.count + oRng.Comments.count = 0 Then lastCommentOrRevision = True
    End Function

Tags for this Thread

Posting Permissions

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