Find and Replacing Multiple Strings in a Word Document
top of page

Find and Replacing Multiple Strings in a Word Document


You can use the below macro to find and replace multiple strings in a Word document. Thanks to Graham Mayor for posting it here.

First create a Word table with the values you want to replace in the first column, and the value you want to add in the second column. Save and close it.

Plug the below macro into a new module in Visual Basic. Enter a path to the Word table which contains the find and replace pairs.

Enter the below VBA code in a macro and run it.

Sub ReplaceFromTableList() ' from Doug Robbins, Word MVP, Microsoft forums, Feb 2015, based on another macro written by Graham Mayor, Aug 2010 Dim oChanges As Document, oDoc As Document Dim oTable As Table Dim oRng As Range Dim rFindText As Range, rReplacement As Range Dim i As Long Dim sFname As String 'Change the path in the line below to reflect the name and path of the table document sFname = "C:\FooFolder\findreplace.docx" Set oDoc = ActiveDocument Set oChanges = Documents.Open(FileName:=sFname, Visible:=False) Set oTable = oChanges.Tables(1) For i = 1 To oTable.Rows.Count Set oRng = oDoc.Range Set rFindText = oTable.Cell(i, 1).Range rFindText.End = rFindText.End - 1 Set rReplacement = oTable.Cell(i, 2).Range rReplacement.End = rReplacement.End - 1 Selection.HomeKey wdStory With oRng.Find .ClearFormatting .Replacement.ClearFormatting .MatchWildcards = True .Text = rFindText.Text .Replacement.Text = rReplacement.Text .Forward = True .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Next i oChanges.Close wdDoNotSaveChanges End Sub

The Word document is updated. Note that the macro is case sensitive.


bottom of page