简体   繁体   English

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

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

I have an MS Word document including a table. 我有一个MS Word文档,包括一个表。 I am trying to find and replace text via VBA using the following code: 我试图使用以下代码通过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 outside of the table. 这适用于替换表格之外的所有内容。 But it will not replace any of the content within the table. 但它不会取代表中的任何内容。

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.Range instead of the Selection object. 如果您的目标是在整个文档中执行替换(它从代码中看起来如此,但它不明确),我建议您使用Document.Range而不是Selection对象。 Using Document.Range will make sure everything is replaced, even inside tables. 使用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

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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM