Finding and Replacing a Certain Word from 100+ Documents

M

Marilyn

Hello,

I have been put in charge of a Major task and I was wondering if there was
an easier way of automating that task. All of the documents listed below are
in Word 2007 format.

I have 59 folders and within each folder I have about 50+ documents with
policies and procedures. I now have to go through each of those folders and
each of those documents and look for a certain word and replace it with
another. I know I can use FIND & REPLACE but I was wondering if there is an
easier way of finding and replacing that word in all of those Word 2007
documents without having to go through each one individually.

Thanks in advanced for the help.
 
J

Jonathan West

Marilyn said:
Hello,

I have been put in charge of a Major task and I was wondering if there was
an easier way of automating that task. All of the documents listed below
are
in Word 2007 format.

I have 59 folders and within each folder I have about 50+ documents with
policies and procedures. I now have to go through each of those folders
and
each of those documents and look for a certain word and replace it with
another. I know I can use FIND & REPLACE but I was wondering if there is
an
easier way of finding and replacing that word in all of those Word 2007
documents without having to go through each one individually.

Thanks in advanced for the help.


Hi Marilyn

Take a look at this article


Find & ReplaceAll on a batch of documents in the same folder
http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm
 
M

Marilyn

Hi Jonathan,

Thank you so much for the quick response. I reviewed the code, but I'm not
sure which lines I'm supposed to change. Would you be able to point me in
the right direction once again. Here's the code from the site.

Option Explicit

Public Sub BatchReplaceAll()

Dim FirstLoop As Boolean
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long

PathToUse = "C:\Test\"

'Error handler to handle error generated whenever
'the FindReplace dialog is closed

On Error Resume Next

'Close all open documents before beginning

Documents.Close SaveChanges:=wdPromptToSaveChanges

'Boolean expression to test whether first loop
'This is used so that the FindReplace dialog will
'only be displayed for the first document

FirstLoop = True

'Set the directory and type of file to batch process

myFile = Dir$(PathToUse & "*.doc")

While myFile <> ""

'Open document
Set myDoc = Documents.Open(PathToUse & myFile)

If FirstLoop Then

'Display dialog on first loop only

Dialogs(wdDialogEditReplace).Show

FirstLoop = False

Response = MsgBox("Do you want to process " & _
"the rest of the files in this folder", vbYesNo)
If Response = vbNo Then Exit Sub

Else

'On subsequent loops (files), a ReplaceAll is
'executed with the original settings and without
'displaying the dialog box again

With Dialogs(wdDialogEditReplace)
.ReplaceAll = 1
.Execute
End With

End If

'Close the modified document after saving changes

myDoc.Close SaveChanges:=wdSaveChanges

'Next file in folder

myFile = Dir$()

Wend

End Sub

Thanks,
 
J

Jonathan West

Marilyn said:
Hi Jonathan,

Thank you so much for the quick response. I reviewed the code, but I'm
not
sure which lines I'm supposed to change. Would you be able to point me in
the right direction once again. Here's the code from the site.

Required changes are indicated inline
Option Explicit

Public Sub BatchReplaceAll()

Dim FirstLoop As Boolean
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long

PathToUse = "C:\Test\"

Change the line above so it contains the folder where you have your
documents. Make sure you include the \ character at the end.
'Error handler to handle error generated whenever
'the FindReplace dialog is closed

On Error Resume Next

'Close all open documents before beginning

Documents.Close SaveChanges:=wdPromptToSaveChanges

'Boolean expression to test whether first loop
'This is used so that the FindReplace dialog will
'only be displayed for the first document

FirstLoop = True

'Set the directory and type of file to batch process

myFile = Dir$(PathToUse & "*.doc")

Change the line above to this, because you are using Word 2007 documents

myFile = Dir$(PathToUse & "*.docx")
While myFile <> ""

'Open document
Set myDoc = Documents.Open(PathToUse & myFile)

If FirstLoop Then

'Display dialog on first loop only

Dialogs(wdDialogEditReplace).Show

FirstLoop = False

Response = MsgBox("Do you want to process " & _
"the rest of the files in this folder", vbYesNo)
If Response = vbNo Then Exit Sub

Else

'On subsequent loops (files), a ReplaceAll is
'executed with the original settings and without
'displaying the dialog box again

With Dialogs(wdDialogEditReplace)
.ReplaceAll = 1
.Execute
End With

End If

'Close the modified document after saving changes

myDoc.Close SaveChanges:=wdSaveChanges

'Next file in folder

myFile = Dir$()

Wend

End Sub

By the way, make a copy of your documents and work on the copy. This is just
in case the change goes horribly wrong and you need to throw away the
documents and start again. You still have a set of originals in pristine
condition, and you can make another copy if necessary.
 
M

Marilyn

Hello,

Thanks again.... I'm not a macro expert so I'm having a tough time getting
the macro to work. I looked over the site again and figured the second macro
would work best. Once again I'm making the changes I think I need to make
based on the location of my files, but It does not work. Please let me know
what I'm doing wrong.... Thanks.... PS: Yes, I had already created a copy of
the files to work with.

Below is the second code with the changes that I made.

Option Explicit

Public Sub BatchReplaceAll()

Dim FirstLoop As Boolean
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long
Dim i As Long

PathToUse = "C:\policies\admissions"

'Error handler to handle error generated whenever
'the FindReplace dialog is closed

On Error Resume Next

'Close all open documents before beginning

Documents.Close SaveChanges:=wdPromptToSaveChanges

'Boolean expression to test whether first loop
'This is used so that the FindReplace dialog will
'only be displayed for the first document

FirstLoop = True

'Set the directory and type of file to batch process

With Application.FileSearch
.NewSearch
.LookIn = "c:\policies\admissions"
.SearchSubFolders = True
.FileName = "*.doc"
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles

If .Execute() Then

For i = 1 To .FoundFiles.Count

'Open document
Set myDoc = Documents.Open(.FoundFiles(i))

If FirstLoop Then

'display dialog on first loop only

Dialogs(wdDialogEditReplace).Show

FirstLoop = False

Response = MsgBox("Do you want to process " & _
"the rest of the files in this folder", vbYesNo)
If Response = vbNo Then Exit Sub

Else

'On subsequent loops (files), a ReplaceAll is
'executed with the original settings and without
'displaying the dialog box again

With Dialogs(wdDialogEditReplace)
.ReplaceAll = 1
.Execute
End With

End If

'Close the modified document after saving changes
myDoc.Close SaveChanges:=wdSaveChanges

Next i

End If

End With

End Sub


Thanks,
 
G

Graham Mayor

It is not broken I just opened it from the message?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

Marilyn

Hi Greg,

I tried your add-in on one of the folders and it worked great! the only
problem I ran into is that some of the documents are protected forms, and
when the code encounters a protected file it stops. My way around this is to
unprotect those files for the process to run.

Graham, initially the link did not work for me, but I finally got it to open
the page..... Thanks for your help as well.
 
M

Marilyn

Hi Greg,

I re-downloaded the template and tried it again but I still get the
following error:
Run-time error '5485': The Password is incorrect.

The file it stops in is a Word form and the Form fields are password
protected.

Thanks,
 
G

Greg Maxey

Marilyn,

So you mean the form is both protected and requires a password to unprotect.
Right?

In that case, yes it will fail as you indicate.
 
M

Marilyn

Ah yes... Now I get a prompt to enter password. You sir are a genius.... :)
Here’s something to ponder. What is the person did not know the password,
can clicking the Cancel button tell the program to skip that file and move on
to the next. Perhaps the code can skip all password protected file and in
the end let the user know x amount of files were skipped due to password
constraints.

Thanks a bunch for sharing your code with all of us.
 
G

Greg Maxey

Marilyn,

Suggestions like yours are simliar to ideas like mine. Both are usually the
root cause of my intended simple projects turning into unmanageble Medusas
;-)

Check back to the website from time to time. When I get time I may explore
the idea.
 
M

Marilyn

Good Morning Greg,

Beautiful Work......I tried it and I love the message you get when clicking
the Cancel button. I've always wanted to learn more about macros and
programming, but..... :-(

Once again you did an outstanding job with the code and sharing it with all
of us. Write back if you get a chance and let me know what books I can pick
up on VBA in MS Word.

Thanks
 
G

Greg Maxey

Marilyn,

Thanks.

I have never read a book on VBA. What I know has been picked up along the
way through participating in the Word VBA newsgroups where it tool a lot of
spoon feeding in the beginning ;-) and I enjoy trying to find logical
solutions to problems and VBA is a handy scratchpad for that hobby. When
asked how to fly an F-18 fighter a pilot once replied "You get in the
cockpit and operate the controls." To me learning VBA is the same.
Stepping through existing code using the F8 key is a good way to see what is
going on with each commmand.

That said, after spending a year or more trying get my head around the
RibbonX and controls used in Word2007, I did purchase the book "RibbonX,
Customizing the Office 2007 Ribbon." I bought it used online for less than
$15.00 and it has a fairly indepth and easy to understand secion on VBA.

Cheers
 

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