使用Microsoft.Office.Interop.Word中的Selection.Find查找文档中的跟踪更改

时间:2009-04-15 20:53:20

标签: vb.net ms-word selection-object

我目前正在VB.Net应用程序中使用以下代码来查找Word文档中的特定文本。文本由.Text语句中的字符代码表示的符号包围。下面的代码工作正常。现在的问题是,有时文档中的所需文本已被标记为删除,并在文档中显示为跟踪更改。我只想找到尚未标记为删除的所需文本。有没有人知道确定找到的文本是否删除的方法?

    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

3 个答案:

答案 0 :(得分:0)

对我而言,它可以在搜索之前将修订视图设置为final。然后只找到最终修订版中可见的文本(您可以备份之前的值并在搜索完成后恢复视图):

ActiveDocument.Windows(1).View.RevisionsView = wdRevisionsViewFinal

完整代码:

' 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 

答案 1 :(得分:0)

我最终循环遍历每个修订版并更改已删除修订版的字体颜色,以区别于未删除的评论,如下所示:

    For Each xRevision In theDoc.Revisions
        If xRevision.Type = Word.WdRevisionType.wdRevisionDelete Then
            xRevision.Range.Font.Color = Word.WdColor.wdColorBlack
        End If
    Next

然后我可以根据字体颜色进行查找和处理找到的注释:

    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

答案 2 :(得分:0)

尝试在搜索或处理Word文档之前关闭显示修订版本:

document.ShowRevisions = false;

它会保留文档中的更改跟踪(如果存在),但是只允许您查看和处理最新内容,而不是任何删除等。