PDA

View Full Version : [SOLVED:] How to know if there are any more comments or revisions in a Word document?



johndavidson
07-28-2017, 04:20 AM
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!

mana
07-29-2017, 01:54 AM
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

johndavidson
07-29-2017, 02:07 AM
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!

gmaxey
07-29-2017, 05:23 AM
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

mana
07-29-2017, 05:51 AM
Thanks!
I always learn from your codes.

johndavidson
07-29-2017, 01:40 PM
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