在word文档中找不到文本,如果文本具有使用vba的字段索引



>我需要在word 2007中找到以下文本

布冯{XE "布冯" } 1997

我使用选择方法在 VBA 中尝试过,它工作正常。我使用范围执行此操作,但它不起作用。谁能帮我?

使用选择方法的代码:

ActiveDocument.ActiveWindow.View.ShowAll = False
ActiveDocument.ActiveWindow.View.ShowHiddenText = False
Selection.Find.ClearFormatting
With Selection.Find
    .Text = "Buffon 1997"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
If Selection.Find.Execute Then
    Selection.Range.Font.Bold = True
End If

它工作正常。

使用word范围方法的代码:

Dim doc As Document, story As Range, tem As Range
Set doc = ActiveDocument
For Each story In doc.StoryRanges
    Set tem = story.Duplicate
    tem.Find.ClearFormatting
    With tem.Find
        .Text = "Buffon 1997"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .IgnorePunct = True
    End With
    If tem.Find.Execute() Then
        tem.Font.Bold = True
    End If
Next story

此代码不起作用。

我在这里写了一个关于在Word中搜索每个story的类似答案: Word 宏查找 Word 文档中的所有内容并将其替换为文本框

有关如何搜索每个story的文档如下:http://word.mvps.org/faqs/customization/ReplaceAnywhere.htm

您应该能够跳过Set tem = story.Duplicate行,只需跳到With story.Find

最新更新