Log in

View Full Version : Best way for multiple find/replace macro?



translator_
10-10-2012, 11:33 AM
I tend to use this snippet for multiple find/replace operations, I wonder though what is the best and quickest way. Preferably a way that would allow me to easily incorporate find/replace lists?

.Text = "texttofind"
.Replacement.Text = "texttoreplace"
.MatchWholeWord = False
.Execute Replace:=wdReplaceAll

gmaxey
10-10-2012, 12:05 PM
I don't know if it is best, but I certainly like it:
http://gregmaxey.mvps.org/word_tip_pages/vba_find_and_replace.html

translator_
10-10-2012, 12:11 PM
Yes, thanks, I know it, very good work!

What I am looking though is find/replace operations (or some sort of VBA "engine" that would facilitate this) I could incorporate into VBA code.

gmaxey
10-10-2012, 12:13 PM
Try using an array:

Sub FRUsingAnArray1()
Dim SearchArray As Variant
Dim myRange As Range
Dim i As Long
Dim TermString As String
SearchArray = Array("the", "men", "good")
ResetFRParameters
Set myRange = ActiveDocument.Range
For i = 0 To UBound(SearchArray)
TermString = SearchArray(i)
With myRange.Find
.Text = TermString
.Replacement.Font.Color = wdColorGreen
.Replacement.Text = TermString
.MatchWholeWord = True
.Execute Replace:=wdReplaceAll
End With
Next
End Sub

translator_
10-13-2012, 09:10 AM
Thanks. But where do I define the replacement words?

fumei
10-13-2012, 12:47 PM
The same place you define the search words...in an array.

OR, you can use (for both if you like), a text file.

macropod
10-13-2012, 03:05 PM
translator: I have posted numerous solutions for this kind of thing, using everything from hard-coded arrays, to text files, documents and Excel worksheets, both on this site and elsewhere. A few minutes searching this forum or the web generally would turn up lots of solutions, rather than asking people to find them for you.

gmaxey
10-13-2012, 03:28 PM
Seconding what Paul has said.

If you feel the Find and Replace add-in is very good work, then just open it, open the code and use what you want.

fumei
10-13-2012, 04:16 PM
Tripling that: not just Paul. There are numerous postings by quite a few different people on this, all over the place. It comes up regularly.