Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / General MS Word Questions / January 2004

Tip: Looking for answers? Try searching our database.

Search and replace in several documents

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dr. Indera - 09 Jan 2004 23:26 GMT
hello,

i'd like to know if it's possible or if there is an add-on that will let you
search and replace across multiple documents.

thanks
indera
Greg Maxey - 10 Jan 2004 00:38 GMT
Dr. Indera,

I found this macro in Google.  If all of your files you want to search are
in a common folder this macro will ask for the folder location, then the
search text, then the replace text.

Sub SearchReplaceMultipleDocs()

' run search and replace in all docs in a folder

Dim Title, Message1, Message2, Message3, myPath, myLookFor, myReplace
Title = "MultiDocument Search and Replace"
Message1 = "Enter folder path."
Message2 = "Enter text to be found."
Message3 = "Enter replacement text."
myPath = InputBox(Message1, Title)
myLookFor = InputBox(Message2, Title)
myReplace = InputBox(Message3, Title)

With Application.FileSearch
.LookIn = myPath  '"C:\Documents\How to\test" ' where to search
.SearchSubFolders = False ' search the subfolders
.FileName = "*.doc" ' file pattern to match

' if more than one match, execute the following code
If .Execute() > 0 Then
' to display how many files this macro will access,
' uncomment the next line of code
MsgBox "Found " & .FoundFiles.Count & " file(s)."
End If

' for each file you find, run this loop
For i = 1 To .FoundFiles.Count
' open the file based on its index position
Documents.Open FileName:=.FoundFiles(i)

' search and replace
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = myLookFor  '"the"
.MatchCase = True
.Replacement.Text = myReplace  '"THE"
End With
Selection.Find.Execute Replace:=wdReplaceAll

'' a second search and replace
'With Selection.Find
'.Text = "OldStreetAddress"
'.Replacement.Text = "NewStreetAddress"
'End With
'Selection.Find.Execute Replace:=wdReplaceAll
'

' save and close the current document
ActiveDocument.Close wdSaveChanges
Next i
'Else
' if the system cannot find any files
' with the .doc extension
'MsgBox "No files found."
'End If
End With
End Sub

Signature

Greg Maxey
A peer in "peer to peer" support
Rockledge, FL
To e-mail, edit out the "w...spam" in gmaxey@whamspammvps.org

> hello,
>
[quoted text clipped - 3 lines]
> thanks
> indera
Dr. Indera - 12 Jan 2004 05:32 GMT
thanks greg, i'll give it a try.

indera

> Dr. Indera,
>
[quoted text clipped - 74 lines]
> > thanks
> > indera
Suzanne S. Barnhill - 10 Jan 2004 22:48 GMT
See http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Signature

Words into Type
Fairhope, Alabama USA
Word MVP FAQ site: http://www.word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


> hello,
>
[quoted text clipped - 3 lines]
> thanks
> indera
Dr. Indera - 12 Jan 2004 05:33 GMT
hi suzanne,

i will try this one also.

thanks
indera

> See http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm
>
[quoted text clipped - 15 lines]
> > thanks
> > indera
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.