vba 在 MS Word 2010(包括表格)中查找和替换整个文档中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18609963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:33:18  来源:igfitidea点击:

To find and replace a text in the whole document in MS Word 2010 (including tables)

vbams-word

提问by 122user321

I have an MS Word document including a table. I am trying to find and replace text via VBA using the following code:

我有一个包含表格的 MS Word 文档。我正在尝试使用以下代码通过 VBA 查找和替换文本:

If TextBox1.Text <> "" Then
    Options.DefaultHighlightColorIndex = wdNoHighlight
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Highlight = True
    With Selection.Find
        .Text = "<Customer_Name>"
        .Replacement.Text = TextBox1.Text
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

Selection.Find.ClearFormatting
    With Selection.Find.Font
    .Italic = True
    End With
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find.Replacement.Font
    .Italic = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    End If

This works fine for replacing all my content which is outsideof the table. But it will not replace any of the content within the table.

这适用于替换我在表格之外的所有内容。但它不会替换表格中的任何内容。

回答by d-stroyer

If your goal is to perform replacements in the whole documents (it looks so from the code, but it is not explicit), I would suggest you use Document.Rangeinstead of the Selectionobject. Using Document.Rangewill make sure everything is replaced, even inside tables.

如果您的目标是在整个文档中执行替换(从代码看起来是这样,但并不明确),我建议您使用Document.Range而不是Selection对象。使用Document.Range将确保所有东西都被替换,即使是在桌子里面。

Also, it is more transparent to the user, as the cursor (or selection) is not moved by the macro.

此外,它对用户更加透明,因为光标(或选择)不会被宏移动。

Sub Test()
  If TextBox1.Text <> "" Then
    Options.DefaultHighlightColorIndex = wdNoHighlight
    With ActiveDocument.Range.Find
      .Text = "<Customer_Name>"
      .Replacement.Text = TextBox1.Text
      .Replacement.ClearFormatting
      .Replacement.Font.Italic = False
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute Replace:=wdReplaceAll
    End With
  End If
End Sub

回答by 122user321

I have used the following code and it works like charm..... for all the occurances that are found in the document.

我使用了以下代码,它的作用就像魅力..... 对于文档中发现的所有事件。

  stringReplaced = stringReplaced + "string to be searched"
For Each myStoryRange In ActiveDocument.StoryRanges
    With myStoryRange.Find
        .Text = "string to be searched"
        .Replacement.Text = "string to be replaced"
        .Wrap = wdFindContinue
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Highlight = False
        .Execute Replace:=wdReplaceAll
    End With
Next myStoryRange