Find and replace

M

MG

Is there any way to find a text string and replace it in
multiple documents without going into each one?

Thanks!

M
 
G

Guest

ok, I created the template, and have it globally
selected. but the macro doesn't run after I do a
find/replace .. any suggestions?
 
J

Jay Freedman

Now that I've reread that article, I notice that it omits a very
important step:

In the code, the 8th line from the top says

PathToUse = "C:\Test\"

You have to change the "C:\Test\" part to the path of the folder where
you stored your documents (or else create a folder named "C:\Test\"
and copy your documents into it).

The macro should probably be fixed up to display a dialog to let you
pick a folder each time you run it, but in the meantime you have to
make this change in the code.
 
M

MG

ok, I still am unclear how the find/replace dialog calls
this sub when you use it. I am using office 2003, it is
in a folder called Test on C, and still only replacing
(or giving me the options for) one document. The code is
in a macro and I see it in every new document I open.

Thanks for your help!
 
J

Jay Freedman

The find/replace dialog does not call the macro -- it's the other way
'round. When you run the macro, the line

Dialogs(wdDialogEditReplace).Show

in the code causes the replace dialog to appear. When you enter your
information there and click OK, the rest of the macro resumes
execution from that line downward.

If you're bringing up the Replace dialog from the menu or a shortcut
key and expecting that to run the macro, it won't happen. You need to
start the macro from the Tools > Macro > Macros dialog (shortcut
Alt+F8).
 
M

MG

ok, that worked pretty well. The only thing is it did
not work in the headers/footers like find/replace usually
does. Is there any way to correct this?

thanks again.
 
G

Graham Mayor

The following should work for the headers/footers:

Public Sub BatchReplaceAllInHeaderFooter()
Dim FirstLoop As Boolean
Dim myFile As String
Dim PathToUse As String
Dim myDoc As Document
Dim Response As Long
Dim oSection As Section
Dim oHeader As HeaderFooter
Dim oFooter As HeaderFooter

PathToUse = "D:\My Documents\Temp\"
On Error Resume Next
Documents.Close Savechanges:=wdPromptToSaveChanges
FirstLoop = True
myFile = Dir$(PathToUse & "*.doc")

While myFile <> ""
Set myDoc = Documents.Open(PathToUse & myFile)
If FirstLoop Then
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

With Dialogs(wdDialogEditReplace)

For Each oSection In ActiveDocument.Sections

For Each oHeader In oSection.Headers
If oHeader.Exists Then
.ReplaceAll = 1
.Execute
End If
Next oHeader

For Each oFooter In oSection.Footers
If oFooter.Exists Then
.ReplaceAll = 1
.Execute
End If
Next oFooter

Next oSection
End With
End If

myDoc.Close Savechanges:=wdSaveChanges
myFile = Dir$()

Wend
End Sub
 

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