find, replace, and delete from a list

J

John Smith

How do I "find and delete" or "find and replace" using a keyword
list? For example, if I want to find "(space)From(space)" and
replace it with "(tab)From(tab)","(space)to(space)" with
"(tab)to(tab)",...... and I want to store the "find string" and
"replace string" in a file. Can this be done?

Also, can I store a list of keywords in a file and do a "find and
delete the whole line" from the file?
 
D

Doug Robbins

The following will do what you want if the words to be "replaced" are in
separate paragraphs in the document that is saved with the name Replace
List:

Dim source As Document, target As Document, i As Long, FindWhat As Range
Set target = ActiveDocument
Set source = Documents.Open("c:\documents\replace list.doc")
target.Activate
For i = 1 To source.Paragraphs.Count
Set FindWhat = source.Paragraphs(i).Range
FindWhat.End = FindWhat.End - 1
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=" " & FindWhat.Text & " ",
ReplaceWIth:=vbTab & FindWhat & vbTab, MatchWildcards:=False,
Wrap:=wdFindContinue, Forward:=True) = 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
 
J

John Smith

Works like a charm. Thank you.

BTW, how do I close the "replace list.doc"? I tried
"Documents("c:\documents\replace list.doc").Close". It did close
the file but left a Word window open (a blank MSWord window). How
do I close that blank window?
 
D

Doug Robbins

I should have added a

source.Close wdDoNotSaveChanges

to close the document with the list.

--
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
 

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