Link to home
Start Free TrialLog in
Avatar of JohnRobinAllen
JohnRobinAllenFlag for Canada

asked on

VBA Word Can I find out if a document has any tracked changes?

With VBA and a Word document, I need to know if a document has any tracked changes so that I can accept or reject each change. I thought ActiveDocument.Revisions.Count would tell me how many I have, but it does not.

A kludge is to use
     On Error Resume Next
     WordBasic.NextChangeOrComment

and if that returns an error, then I have no more changes or comments.

Surely there must be a more elegant way of solving that problem.

    Thanks, as always, for the help.
     j.r.a.
Avatar of JohnRobinAllen
JohnRobinAllen
Flag of Canada image

ASKER

I tried my kludge with this function. It works but displays an annoying error message. I can live with the kludge but not with the error message being displayed to a user who will have no idea what it is talking about.

I certainly hope there is a solution out there somewhere.

         j.r.a.


Private Function NextChangeOrComment() As Boolean
On Error Resume Next
      WordBasic.NextChangeOrComment
      If Err.Number = 0 Then
            NextChangeOrComment = True
      Else
            NextChangeOrComment = False
      End If
End Function

Open in new window

SOLUTION
Avatar of atomsheep
atomsheep

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GrahamSkan
The Revsions.Count should work. It does for me:

Dim rv As Revision
Debug.Print ActiveDocument.Revisions.COUNT
For Each rv In ActiveDocument.Revisions
    Debug.Print rv.Range.Text
Next rv
Hmm.
My WordBasic manual doesn't mention NextChangeOrComment. However, I got a 'Do you want to resume at the start of the document' message. Is that your problem message? Try selecting the whole document first.

Private Function NextChangeOrComment() As Boolean
ActiveDocument.Select '<--------
On Error Resume Next
      WordBasic.NextChangeOrComment
      If Err.Number = 0 Then
            NextChangeOrComment = True
      Else
            NextChangeOrComment = False
      End If
End Function

Open in new window

I agree with Graham that  Revsions.Count should work. My problem is in a footnote, which apparently the Revision.Count ignores. The attached excerpt from a document shows a newly-added footnote. A macro named "Test" displays the Revision.Count as zero, yet there is a tracked change in the document in that added footnote. Perhaps others do not get the same result that I get.

I will temporarily use Graham's suggestion for my kludge: Application.DisplayAlerts = wdAlertsNone, but I will first leave the question open for a day or two more to see if there is not something simpler that would correct the Revision.Count and not have the computer waste its time with my NextChangeOrComment function.

          j.r.a.


Test-document-with-tracked-chang.doc
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Both Graham Skan's "CountAllRevs" and Atomsheep's supplement to my original kludge work well.

I am awarding the 300 points to Graham's 12-line complete solution, but another 100 points go to Atomsheep for his two additions to my kludge:
     Application.DisplayAlerts = wdAlertsNone
     Application.DisplayAlerts = wdAlertsAll
Two solutions and each works well. Graham's solution is complete and elegant. Atomsheep's two lines make my kludge work well too.
    Many thanks to both gentlemen.
          j.r.a.