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

发布于 2024-07-17 07:36:05 字数 928 浏览 4 评论 0原文

我目前正在 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黄昏下泛黄的笔记 2024-07-24 07:36:05

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

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 

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 
自找没趣 2024-07-24 07:36:05

我最终循环遍历每个修订版本并更改已删除修订版本的字体颜色,以将它们与未删除的注释区分开来,如下所示:

    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

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
又爬满兰若 2024-07-24 07:36:05

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

document.ShowRevisions = false;

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

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文