views:

1765

answers:

2

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
A: 

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
0xA3
A: 

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