Log in

View Full Version : VBA code to change revisions/markup to highlight in a table



johndavidson
08-14-2013, 10:10 PM
I have a routine that globally accepts all track changes revisions/markup and highlights the changed text. It works fine -- except in tables, when I get the message Run-time error: 5852 "Requested Object is not available" when execution hits the statement ".Range.HighlightColorIndex = wdYellow". What do I have to do to the code to make it work in tables? A test file with markups in a table is attached.

The routine reads:

Sub MarkupToHighlight()

Dim myRevision As Revision
Dim currDoc As Word.Document

Set currDoc = Application.ActiveDocument

currDoc.ActiveWindow.Selection.HomeKey Unit:=wdStory

Options.DefaultHighlightColorIndex = wdYellow

For Each myRevision In currDoc.Revisions
With myRevision
.Range.HighlightColorIndex = wdYellow
.Accept
End With
Next

End Sub

Can't see how to format the above code - apologies.

Later ... Further tests reveal that the problem occurs when the revision includes the end of cell marker. If the end of cell marker is not included in the revision, the above code works fine.

Thanks

John Davidson

johndavidson
08-15-2013, 03:23 AM
Later ... Further tests reveal that the problem occurs when the revision includes the end of cell marker. If the end of cell marker is not included in the revision, the above code works fine.

I've got the code working in the main text story by modifying the code to check for the existence of tables, and if present to make the change in another, and more long-winded manner.



If currDoc.Tables.Count > 0 Then


While currDoc.Revisions.Count > 0


WordBasic.NextChangeOrComment
Selection.Range.Revisions.AcceptAll
Selection.Range.HighlightColorIndex = wdYellow
Selection.Start = Selection.End

Wend

Else


With currDoc


For Each myRevision In .Revisions ' This method fails inside tables if the end of cell marker is also subject to a revision


With myRevision
.Range.HighlightColorIndex = wdYellow
.Accept
End With

Next

End With

End If


So far so good ... but it takes a lot longer, and it would be better if anyone knows a way to make the original code work if an end cell marker is also subject to a revision, or if the "WordBasic.NextChangeOrComment" method could be speeded up.

Thanks

John Davidson

gmaxey
08-15-2013, 07:36 AM
Probably not the solution you are looking before but perhaps combining the two methods would at least enhance speed:


Sub MarkupToHighlight()
Dim myRevision As Revision
Dim currDoc As Word.Document
Dim lngIndex As Long
Set currDoc = Application.ActiveDocument
currDoc.ActiveWindow.Selection.HomeKey Unit:=wdStory
Options.DefaultHighlightColorIndex = wdYellow
For lngIndex = currDoc.Revisions.Count To 1 Step -1
Set myRevision = currDoc.Revisions(lngIndex)
With myRevision
On Error Resume Next
.Range.HighlightColorIndex = wdYellow
.Accept
End With
Next
On Error GoTo 0
While currDoc.Revisions.Count > 0
WordBasic.NextChangeOrComment
Selection.Range.Revisions.AcceptAll
Selection.Range.HighlightColorIndex = wdYellow
Selection.Start = Selection.End
Wend
End Sub

johndavidson
08-15-2013, 09:18 PM
Hi Greg

Thanks for that, but could you let me know what the code is doing? I'm probably not as proficient a VBA programmer as you are assuming!

Also, there is another issue. I'd simplified the actual code I'm using for the sake of the post. In fact, I also need to make a choice of highlight colours by checking the contents of the .FormatDescription of each revision - which I have discovered results in the same error as before if the end of cell marker is a part of the revision. The code presently looks like this:


If ActiveDocument.Tables.Count > 0 Then


'For lngIndex = ActiveDocument.Revisions.Count To 1 Step -1


'Set myRevision = ActiveDocument.Revisions(lngIndex)
'With myRevision


'On Error Resume Next
'.Range.HighlightColorIndex = wdBrightGreen
'.Accept

'End With

'Next
'On Error GoTo 0
While ActiveDocument.Revisions.Count > 0


WordBasic.NextChangeOrComment
'If InStr(Selection.Range.Revisions.Item(1).FormatDescription, "Highlight") Then


'Selection.Range.HighlightColorIndex = wdTurquoise

'Else


Selection.Range.HighlightColorIndex = wdBrightGreen

'End If
Selection.Range.Revisions.AcceptAll
Selection.Start = Selection.End

Wend

Else


For Each myRevision In ActiveDocument.Revisions


With myRevision


If mhSw = True Then


If InStr(myRevision.FormatDescription, "Highlight") Then


.Range.HighlightColorIndex = wdTurquoise

Else


.Range.HighlightColorIndex = wdBrightGreen

End If


.Accept

End If

End With

Next

End If

The code that checks the .FormatDescription is presently commented out, and all revisions go to bright green if there are any tables in the document.


'If InStr(Selection.Range.Revisions.Item(1).FormatDescription, "Highlight") Then


'Selection.Range.HighlightColorIndex = wdTurquoise

'Else


Selection.Range.HighlightColorIndex = wdBrightGreen

'End If

Maybe the Selection.Range.Revisions.Item(1).FormatDescription has wrong syntax. Being somewhat uncertain what I'm doing, I also tried Selection.Range.Revisions(1).FormatDescription, but both result in errors.

Do you have any suggestions on how to check the FormatDescription when there are tables in the document with end of cell markers being a part of a revision?

Another possibility might be to pre-process all tables and remove end of cell markers from the revision, if such a thing is possible. This routine is a part of larger routine (not written by me) that corrects the italicization of punctuation around italicized text according to publication protocols. All incidences of italicized text get checked and automatically set as revisions, but only if a change is made is the string "Highlight" added to the FormatDescription. If the entire contents of a table cell are italicized then the entire cell gets marked as a revision, including the end of cell marker. The final pass is to accept all revisions and to highlight any changed text, which is what the above routine is doing. It has been working fine until I tried to correct the italicization of a document containing a few tables.

Any ideas/code would be much appreciated.

Thanks

John

gmaxey
08-16-2013, 10:04 AM
John,

Basically the code is using your original method where it works and the WordBasic mehtod where it didn't I think this is clearly a bug in Word 2003, because your original code works fine in Word 2007/2010 and 2013.

johndavidson
08-16-2013, 01:08 PM
Hi Greg

Ah yes, that makes sense. By putting the InStr test into the first pass, the turquoise highlight now gets sets in the majority of instances. Only when in a table cell with the end of cell marker a part of the revision does it end up bright green, which is v. rare.

So issue resolved. Thanks v. much

John