3
votes

I am trying to proofread scientific papers by finding and highlighting common punctuation and spacing errors (such as double spaces, incorrect spacing around punctuation marks, etc.). I am using Word 2010.

The following code works for Word documents that do not have Track Changes turned on. It highlights instances of the strings contained in TargetList in red:

Sub HighlightTargets2()
Dim range As range
Dim i As Long
Dim TargetList

TargetList = Array("  ", " ,", " .", " ?", " :", " ;", " -", " –", " —", "- ", "– ", "— ", ",,", "..", "::", ";;", "??", ",.", ".,", ",?", "?,", "?.", ".?", ";:", ":;", ";,", ";.", ".;", ".;", "^$(", "^$ )", "( ^$", "^#(", "^# )", ")^ #", "( ^#") ' put list of terms to find here
For i = 0 To UBound(TargetList)
    Set range = ActiveDocument.range
    With range.Find
        .Text = TargetList(i)
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        Do While .Execute(Forward:=True) = True
            range.HighlightColorIndex = wdRed
        Loop
    End With
Next
End Sub

I only need to check for such errors in the final view of the document. However, this macro takes "deleted" (as in, deleted but changes not yet accepted) characters as still being present in the document.

Consider this example (ignore the yellow highlights).
enter image description here

I split the original sentence by adding a period and deleting the semicolon. Thus, in Final: Show Markup, the inserted period and the deleted semicolon appear together (.;). Even if I run the macro while on "Final" view (not "Final: Show Markup"), the .Find method will take such "deleted" characters as still being in the text.

The following effect happens.

Wrong Highlight
enter image description here
One possible workaround is to open another instance of Word, copy and paste all the text from the file (after selecting "Final") and running the macro on the new instance of Word. Then, I'd have to look for the highlighted errors on the new instance of Word and manually fix them on the other.

Is there a way to make the .Find method only search on text that appears when viewing the document in "Final" view and ignore text that was deleted while Track Changes was on?

2

2 Answers

0
votes

As Word considered Tracked Changes to be part of the document until changes are accepted, you can accept all tracked changes before executing your code:

Private Sub AcceptAllChanges() Dim stry As Object

For Each stry In ActiveDocument.StoryRanges
    If stry.Revisions.Count >= 1 Then _
    stry.Revisions.AcceptAll
Next

End Sub

0
votes

Not sure if this will address your issues. I think it did for me. At the start of your code put:

    With ActiveWindow.View.RevisionsFilter
        .Markup = wdRevisionsMarkupNone
        .View = wdRevisionsViewFinal
    End With

Then at the end, you can revert back to the options you would have saved first or force to something more sensible such as:

    With ActiveWindow.View
        .RevisionsFilter.Markup = wdRevisionsMarkupSimple
        .RevisionsFilter.View = wdRevisionsViewFinal
        .MarkupMode = wdMixedRevisions
    End With