This is a variation on the multiple find/replace macro covered here: https://cybertext.wordpress.com/2015/03/03/word-macro-to-run-multiple-wildcard-find-and-replace-routines/. I suggest you read that post first (and test it) before attempting this one. This post assumes you can already do what’s in that earlier post.
The scenario for this one is a little different — the author wanted to find various words in a document and highlight them for probable change. As there were MANY documents she had to process, she needed an easy way to find these words and highlight them. She didn’t want to change them at this stage. The words related to project names, company names, facility names, document number prefixes, etc. One project’s documents were to be the basis of a set of documents for another project in the same company, so one of the tasks was to ‘personalize’ the copies of the original project’s documents with the names used by the new project.
Although there may be several ways you can do this, I decided to stick with what I already knew. I figured that using a variation of the ReplaceTableWithList macro (as discussed here: https://cybertext.wordpress.com/2015/03/03/word-macro-to-run-multiple-wildcard-find-and-replace-routines/) and a new table called by that macro variation should solve it. And it did.
Step 1: Create your find/replace table
- Start a new Word document, and create a two-column table in it.
- In the left column, type in the words/phrases you want to find, each on a different row.
- In the right column, type \1. (This means that the wildcard find/replace will replace what was found with itself — remember, you’re only trying to identify the words that *may* need changing at this point, not changing them.)
- Save the document to this folder, noting the name of the file as you’ll need that later:
Step 2: Set up the macro to work with the table
Add the macro below to an existing document, or better, an existing template, or even better, a central macros template that loads whenever you open Word.
Once you’ve added it, change the line that starts with sFname and has the file path — that path points to MY file on MY computer. You need to change it to YOUR file name and YOUR file path.
NOTE: Copy all the code below to the clipboard — it goes off the page, so don’t type it out as you’ll miss some of it or could make a typo.
' 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:\Users\rhonda\AppData\Roaming\Microsoft\Word\STARTUP\find_and_replace_routines_names_macro.docx"
Set oDoc = ActiveDocument
Set oChanges = Documents.Open(FileName:=sFname, Visible:=False)
Set oTable = oChanges.Tables(1)
' Make sure highlight is set to the colour you want, e.g. wdYellow, wdBrightGreen, wdPink, wdTurquoise
Options.DefaultHighlightColorIndex = wdTurquoise
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
.MatchWildcards = True
.Text = rFindText.Text
.Replacement.Text = rReplacement.Text
.Replacement.Highlight = True
.Forward = True
.Wrap = wdFindContinue
Step 3: Test that it works
After setting up the ReplaceFromTableListName macro (above), run it on a test document — copy an existing document and test on the copy to make sure you don’t mess up anything.
- If you get an error message, check that you have the correct file name and path in the macro, AND check that your Word document containing the table that’s called by the macro has no empty rows.
- If none of the words in your table get highlighted in your document on the first pass, select a highlight colour from the Home tab as though you were going to highlight manually, then run the macro again.
- If some words are missed, check the table containing them — if a word has an initial capital in the table, but not in the document you are searching (e.g. in a URL), then the macro won’t highlight it. For words that could be capitalized in various ways, either add new lines for each variation, OR search for the part of the word you know won’t be capitalized (e.g. if you were searching for ‘Amazon’, then change the search term in the first column to ‘mazon’ to pick up the word whether it has an initial cap or not).
- Some areas of your document won’t get highlighted, even if they contain the words you’re looking for — e.g. headers and footers, text boxes. You’ll have to check for these manually.
My author was VERY happy — she had something that only took a few seconds to run and highlighted all the various words she needed to look for.
[Links last checked November 2015]