PDA

View Full Version : How to check if found text is a revision?



velance
09-11-2019, 06:42 PM
Hey everyone,

I have an excel macro that opens a word document with tracked changes, looks for certain values and then replaces them with other values from the excel document. However, with tracked changes, anything deleted or replaced is still retained in the word document and will be picked up by the Find object. I want to make a simple if clause to check if anything found is a revision or not, but I after spending days of searching I haven't been able to find anything that works.

I have played around with the Revisions object and while I can successfully count the number of revisions, everything else just gives me an error. Is it somehow possible to determine if the value found with wdDoc.Content.Find is a revision or not?

Any help would be greatly appreciated.

gmayor
09-11-2019, 09:00 PM
I don't often work with tracked changes so there may be a simpler way, however the following should work. It records the track changes options then turns off tracking, performs the check then restores the original tracking settings. Do what you want with oRng in place of the message box.


Sub Macro1()
'Graham Mayor - https://www.gmayor.com - Last updated - 12 Sep 2019
Const strFind As String = "text to find"
Dim bTrack As Boolean
Dim lngMarkup As Long, lngView As Long
Dim oRng As Range
With ActiveWindow.View.RevisionsFilter
lngMarkup = .Markup
lngView = .View
End With


lngMarkup = ActiveWindow.View.RevisionsFilter.Markup
bTrack = ActiveDocument.TrackRevisions
ActiveDocument.TrackRevisions = False
With ActiveWindow.View.RevisionsFilter
.Markup = wdRevisionsMarkupNone
.View = wdRevisionsViewFinal
End With
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:=strFind)
If oRng.Revisions.Count > 0 And Len(oRng) > 0 Then
MsgBox oRng.Text 'the found text
End If
oRng.Collapse 0
Loop
End With
With ActiveWindow.View.RevisionsFilter
.Markup = lngMarkup
.View = lngView
End With
ActiveDocument.TrackRevisions = bTrack
Set oRng = Nothing
End Sub
Or you could call a function from your code to test the found range e.g.

Public Function IsRevision(oRng As Range) As Boolean
'Graham Mayor - https://www.gmayor.com - Last updated - 12 Sep 2019
Dim bTrack As Boolean
Dim lngMarkup As Long, lngView As Long
If Len(oRng) = 0 Then
oRng.Start = oRng.Words(1).Start: oRng.End = oRng.Words(1).End
End If
With ActiveWindow.View.RevisionsFilter
lngMarkup = .Markup
lngView = .View
End With


lngMarkup = ActiveWindow.View.RevisionsFilter.Markup
bTrack = ActiveDocument.TrackRevisions
ActiveDocument.TrackRevisions = False
With ActiveWindow.View.RevisionsFilter
.Markup = wdRevisionsMarkupNone
.View = wdRevisionsViewFinal
End With


If oRng.Revisions.Count > 0 And Len(oRng) = 0 Then
IsRevision = True
End If
With ActiveWindow.View.RevisionsFilter
.Markup = lngMarkup
.View = lngView
End With
lbl_Exit:
Exit Function
End Function