1
votes

I am required to subject MS Word documents to a third-party software which does not recognize the "track changes" markup. But I still need to keep the crossed out text and the newly added text so that my colleagues know what was the original version and what is the change.

The following macro works if only one person edited the Word document.

Sub Macro1()

Dim chgAdd As Word.Revision

If ActiveDocument.Revisions.Count = 0 Then
    MsgBox "There are no revisions in this document", vbOKOnly
Else
    ActiveDocument.TrackRevisions = False
    For Each chgAdd In ActiveDocument.Revisions
        If chgAdd.Type = wdRevisionDelete Then
            chgAdd.Range.Font.StrikeThrough = True
            chgAdd.Range.Font.Color = wdColorDarkBlue
            chgAdd.Reject
        ElseIf chgAdd.Type = wdRevisionInsert Then
            chgAdd.Range.Font.Color = wdColorRed
            chgAdd.Accept
        Else
            MsgBox ("Unexpected Change Type Found"), vbOKOnly + vbCritical
            chgAdd.Range.Select ' move insertion point
        End If
    Next chgAdd
End If
End Sub

The problem starts when another person edits the already edited document. In this case, the second author may delete the addition by the first author (not the original text). The above macro, instead of removing it, transforms it into the crossed out text which my colleagues mistakenly think was present in the original.

I would like to only convert deleted original text to crossed out text, but not the deleted edit (edit by one author deleted by another author).

Here is an example of how the macro works (properly) when the text is edited by one author.

Figure 1

In "C" you can see that the dark blue crossed out text is what has been deleted from the original text, and red is what has been added.

Now let's look what happens when the text has been edited by two (or theoretically more) different editors, with the macro run at the end (not inbetween):

Figure 2

The problem becomes evident here in "C": The word "plantes" became dark blue crossed out text even though it was not part of the original text.

As you can see, Figure 2-C differs from Figure 1-C. So I want the updated macro to work so that Figure 2-C is same as Figure 1-C.

2
I sort of follow what you want, but am conceptually having difficulty with the logic to be applied. Your current macro turns off track changes - when does it get turned on again? How should the macro know when this state (already edited once) exists? Will the macro run on documents with changes by multiple editors, or only changes from one editor at a time? Which version of Word is involved, here?Cindy Meister
@CindyMeister track changes don't get turned again.It will run on documents with changes by multiple editors (this is what I want too) but the problem is that it does not differentiate between whether the second editor deleted first editor's addition or the original text. I want it to ignore the latter (essentially not accept it)Oposum
I'm still not sure I understand 100%, so need to explore this with you a bit more... 1. There's a document. 2. Someone edits it with TrackChanges. 3. Your code runs and track changes is off. 4. Another person edits but NO track changes. GOAL: You detect the changes made in (4). QUESTION Only to previous changes, or throughtout the document? Why would another person be editing if changes are not wanted? Would it make sense to PROTECT the entire document or the places where your macro has done the changes? Or better to compare a copy of (3) with the result of (4)?Cindy Meister
@CindyMeister the steps are actually in is sequence: 1. There's a document. 2. Someone edits it with TrackChanges. 3. Another person edits but WiTH track changes still on, and during this altere previous editor's tracked changes. 4. The code runs and track changes get turned offOposum
So this is a situation where more than one "editor" has been active in the document and you want to "convert" only one editor's changes, while throwing away the rest of the changes? Or only throwing away changes that are done to the same range of text? If this last is true, will your code handle any other changes by any editor the same way? This information really makes an important difference on the approach used for coding...Cindy Meister

2 Answers

1
votes

The following VBA code loops through a collection of Revisions, checks whether the Revision is an insertion or deletion. If it is and no rejection immediately preceded this part of the loop, then it checks whether the current author was also the author of the previous revision, as there can be no conflict if they're the same.

If they're not the same, then it checks whether the current author is not the main author and whether the current revision is in the same range as the previous, meaning it has "overwritten" a revision by the main author. In this case, the current revision is rejected.

OR, if the author of the previous revision is not the main author and the previous revision is in the same range as the current one, then the previous one has replaced a revision by the main author, then the previous revision is rejected.

On looping, if a revision was just rejected, the code checks whether the new current revision is by an author that is not the main author AND is immediately adjacent to the previous rejection. If that's the case, the new current revision is also rejected.

Then, the code you already have would run after this code has finished.

Sub CompareRevisionsRanges()
  Dim revs As word.Revisions
  Dim rev As word.Revision, revOld As word.Revision
  Dim rngDoc As word.Range
  Dim rngRevNew As word.Range, rngRevOld As word.Range
  Dim authMain As String, authNew As String, authOld As String
  Dim bReject As Boolean

  bReject = False
  Set rngDoc = ActiveDocument.content
  Set revs = rngDoc.Revisions
  If revs.Count > 0 Then
    authMain = revs(1).Author
  Else 'No revisions so...
    Exit Sub
  End If

  For Each rev In revs
    'rev.Range.Select  'for debugging, only
    authNew = rev.Author
    If rev.Type = wdRevisionInsert Or wdRevisionDelete Then
        Set rngRevNew = rev.Range
        'There's only something to compare if an Insertion
        'or Deletion have been made prior to this
        If Not rngRevOld Is Nothing Then
            'The last revision was rejected, so we need to check
            'whether the next revision (insertion for a deletion, for example)
            'is adjacent and reject it, as well
            If bReject Then
                If rngRevNew.Start - rngRevOld.End <= 1 And authNew <> authMain Then
                    rev.Reject
                End If
                bReject = False 'reset in any case
            End If

            'If the authors are the same there's no conflict
            If authNew <> authOld Then
                'If the current revision is not the main author
                'and his revision is in the same range as the previous
                'this means his revision has replaced that
                'of the main author and must be rejected.
                If authNew <> authMain And rngRevNew.InRange(rngRevOld) Then
                    rev.Reject
                    bReject = True
                'If the previous revision is not the main author
                'and the new one is in the same range as the previous
                'this means that revision has replaced this one
                'of the main author and the previous must be rejected.
                ElseIf authOld <> authMain And rngRevOld.InRange(rngRevNew) Then
                    revOld.Reject
                    bReject = True
                End If
            End If
        End If
        Set rngRevOld = rngRevNew
        Set revOld = rev
        authOld = authNew
    End If

  Next        
End Sub
1
votes

You can also convert all changes, then search for and delete all text that has both the underline and strike-through attribute.