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

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. Using Document.Range will make sure everything is replaced, even inside tables.

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

Leave a Comment