'Using Selection.Find in Microsoft.Office.Interop.Word to find tracked changes in a document
I am currently using the code below within a VB.Net application to find specific text in a Word document. The text is surrounded by symbols represented by the character codes in the .Text statement. The code below is working fine. The issue now is that sometimes the desired text within a document has been marked for deletion and appears as tracked change within the document. I would like to find only the desired text that has NOT been marked for deletion. Does anyone know of a way to determine if the found text is a deletion?
xSelection.MoveStart(Word.WdUnits.wdStory)
xSelection.Find.ClearFormatting()
xSelection.Find.Replacement.ClearFormatting()
With xSelection.Find
.Text = ChrW(65000) & "( \[*)" & ChrW(65001)
.Replacement.Text = ""
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
End With
xSelection.Find.Execute(Replace:=Word.WdReplace.wdReplaceNone)
Do While xSelection.Find.Found
........Execute additional code here
Loop
Solution 1:[1]
For me it works to set the revision view to final prior to searching. Then only text visible in the final revision is found (You can backup the previous value and restore the view after your search is done):
ActiveDocument.Windows(1).View.RevisionsView = wdRevisionsViewFinal
Full code:
' set view to show final document revision
' to prevent deleted text from being found
Word.WdRevisionsView revisionsView = xSelection.Document.Windows(1).View.RevisionsView
xSelection.Document.Windows(1).View.RevisionsView = Word.WdRevisionsView.wdRevisionsViewFinal
xSelection.MoveStart(Word.WdUnits.wdStory)
xSelection.Find.ClearFormatting()
xSelection.Find.Replacement.ClearFormatting()
With xSelection.Find
.Text = ChrW(65000) & "( \[*)" & ChrW(65001)
.Replacement.Text = ""
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
End With
xSelection.Find.Execute(Replace:=Word.WdReplace.wdReplaceNone)
Do While xSelection.Find.Found
........Execute additional code here
Loop
' restore previous view
xSelection.Document.Windows(1).View.RevisionsView = revisionsView
Solution 2:[2]
I ended up looping thru each revision and changing the font color of deleted revisions to differentiate them from non-deleted comments like so:
For Each xRevision In theDoc.Revisions
If xRevision.Type = Word.WdRevisionType.wdRevisionDelete Then
xRevision.Range.Font.Color = Word.WdColor.wdColorBlack
End If
Next
Then I can do the find and treat the found comments differently depending on their font color:
xSelection.MoveStart(Word.WdUnits.wdStory)
xSelection.Find.ClearFormatting()
xSelection.Find.Replacement.ClearFormatting()
With xSelection.Find
.Text = ChrW(65000) & "( \[*)" & ChrW(65001)
.Replacement.Text = ""
.Forward = True
.Wrap = Word.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchFuzzy = False
.MatchWildcards = True
End With
xSelection.Find.Execute(Replace:=Word.WdReplace.wdReplaceNone)
Do While xSelection.Find.Found
If xSelection.Font.Color = Word.WdColor.wdColorAutomatic Then
.....
End If
xSelection.Find.Execute()
Loop
Solution 3:[3]
Try to switch off showing revisions prior to searching or processing your Word document:
document.ShowRevisions = false;
It preserves the change tracking in the document if present, but lets you to see and cope only with the newest content, without e.g. any deletions, etc..
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dirk Vollmar |
| Solution 2 | |
| Solution 3 | Mirec J. |
