Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ms-word

vba

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

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.

like image 265
122user321 Avatar asked Sep 04 '13 09:09

122user321


People also ask

What is the use of Find and Replace command in MS word?

Word's Find and Replace function will search your documents for specific text, which can then be highlighted, replaced with different text or formatting, or left as-is. This function provides many advanced options to help make your search as specific as necessary to find what you are looking for.


2 Answers

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
like image 85
d-stroyer Avatar answered Sep 28 '22 07:09

d-stroyer


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  
like image 40
122user321 Avatar answered Sep 28 '22 08:09

122user321