code for a simple search and replace

E

ent

Hi all


what type of code can execute a simple "search and replace all" for a
string in a document?

I need some code that I can associate to a keyboard shurtcut and tha
performs something like:

SEARCH AND REPLACE ALL "the yellow lemon" WITH "the green lime"

thanks!
 
D

Doug Robbins - Word MVP

Ctrl+H

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
E

ent

Doug said:

Yeas, but I need to do the same with some code because I have a list of
string to change and doing it manually takes a lot of time.

Any code example?

Thanks
 
D

Doug Robbins - Word MVP

Dim Findstr As Variant
Dim Replacementstr As Variant
Dim i As Long
Findstr = Split("find1,find2,find3,find4", ",")
'replace find1, etc in the line above with the words that you want to be
replaced
Replacementstr = Split("replace1,replace2,replace3,replace4", ",")
'replace replace1, etc in the line above with the replacement words in the
correct order
For i = 0 To UBound(Findstr)
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=Findstr(i), Forward:=True, _
MatchWildcards:=False, Replacewith:=Replacementstr(i), _
Wrap:=wdFindContinue, MatchCase:=False) = True
Loop
End With
Next i


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top