Search and replace in several documents

  • Thread starter Thread starter Dr. Indera
  • Start date Start date
D

Dr. Indera

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
 
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
 
thanks greg, i'll give it a try.

indera



Greg Maxey said:
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


--
Greg Maxey
A peer in "peer to peer" support
Rockledge, FL
To e-mail, edit out the "w...spam" in (e-mail address removed)
 
Back
Top