Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace text in Multiple Word documents? 1

Status
Not open for further replies.

HendrixD

Technical User
Dec 2, 2008
5
US
I need to replace a couple of paragraphs in about 2200 Word Documents. Does anybody have a suggestion for a solution? I have found several programs that are available but no clue to their quality.

Thank you.
 
This is not really a serious problem. Tedious perhaps, but not inherently difficult.

However, you do not give much in the way of details.

Further, this would require the use of VBA, so please re-post in the VBA forum. Give some useful details, like:

Are these paragraphs together? Separate?

What version of Word - this is important, and you should always mention version when posting.

If a document does NOT have the searched for text (your paragraphs), what do you want to happen?

Are all the files in one folder? Multiple drives?

Just to give you an idea, say I have a bunch of .doc files in C:\ZZZ\Test. Some have the text:

Blah blah blah blah blah.
The quick brown fox jumped over the moon.
More blah blah blah.


And some have the text:

Blah blah blah blah blah.
This does NOT have the text yadda yadda.
More blah blah blah.

Now say you want to replace "The quick brown fox jumped over the moon." with "whatever", but only (of course) for files that actually have "The quick brown fox jumped over the moon."

So the files with: "This does NOT have the text yadda yadda." are to be ignored. The following code does precisely that. It uses FileSearch. Which is why I asked what version of Word. Word 2007 does not have FileSearch available...Microsoft took this away from us.
Code:
Option Explicit

Sub yadda()
Dim j As Long
Dim fs As FileSearch
Dim SearchFor As String
Dim MyReplace As String

SearchFor = "The quick brown fox jumped over the moon."
MyReplace = "whatever"

Set fs = Application.FileSearch
With fs
[COLOR=red] ' collect ONLY files that contain
'  the SearchFor string[/color red]
   .FileType = msoFileTypeWordDocuments
   .LookIn = "c:\zzz\test\"
   .SearchSubFolders = True
   .TextOrProperty = SearchFor
[COLOR=red]' if there are any[/color red]
   If .Execute > 0 Then
      [COLOR=red]' interate through all of them[/color red]
      For j = 1 To .FoundFiles.Count
        [COLOR=red]' open each one[/color red]
         Documents.Open FileName:=.FoundFiles(j)
         [COLOR=red]' and do the Find/Replace[/color red]
         With Selection.Find
            .Text = SearchFor
            .Replacement.Text = MyReplace
            .Execute Replace:=wdReplaceAll
         End With
         [COLOR=red]' save and close[/color red]
         ActiveDocument.Save
         ActiveDocument.Close
      [COLOR=red]' action next file[/color red]
      Next j
   End If
End With  
End Sub
That should get you started. If you have further questions, again, please post to the VBA forum.


faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top