Streamlining a find and replace macro

C

Christine

Hi group,

I was wondering if it was possible to streamline a macro that I have. It
finds and replaces certain text, but does so because I have pasted a copy of
the code below for each instance. Is there a better way to do this?

The code looks like this:

With Selection.Find
.Text = "product"
.Replacement.Text = "productA"
.Forward = True
.Wrap = wdFindAsk
.Format = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Thanks,
Christine
 
C

Cindy M.

Hi Christine,

I don't understand what it is you're asking. What do you mean by "each
instance"?
I was wondering if it was possible to streamline a macro that I have. It
finds and replaces certain text, but does so because I have pasted a copy of
the code below for each instance. Is there a better way to do this?

The code looks like this:

With Selection.Find
.Text = "product"
.Replacement.Text = "productA"
.Forward = True
.Wrap = wdFindAsk
.Format = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
C

Christine

Hi Cindy,

I am using the same code 15 times to replace 15 different things I have to
change.

I am thinking there is a better way of doing it than copying and pasting the
code from my original post 15 times.

Best,
Christine
 
C

Cindy M.

Hi Christine,
I am using the same code 15 times to replace 15 different things I have to
change.

I am thinking there is a better way of doing it than copying and pasting the
code from my original post 15 times.
Thank you for the clarification :)

You can put this code in a separate procedure (Sub), and pass the Find and
Replace values to the procedure. Example:

Sub MainCode()
' your code that needs to process 15 times
' will have 15 calls to the procedure
' each time you pass a different pair of values
' this snippet is for three pairs, only

DoMyFind "product A", "replacement A"
DoMyFind "product B", "replacement B"
DoMyFind "product C", "replacement C"

'Do whatever else you need to do

End Sub

Sub DoMyFind(FindText as String, ReplacementText as String)
With Selection.Find
.Text = FindText
.Replacement.Text = ReplacementText
.Forward = True
.Wrap = wdFindAsk
.Format = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
Top