J
Joe Williams
I've been trying to use this macro (from WordTips)to change my email
address in all the documents in a folder. However, whenever I try to
run it, Word just goes into hourglass-forever mode, and I have to
ctl-alt-del my way out of the program. I don't know much about macros
and have basically cut-and-pasted this into my normal.dot document
which has a couple of other macros that work. Is there something
obviously wrong with this particular macro, or is it just that, as
usual, I don't know what I'm doing?
Thanks for any guidance. Here's the macro:
Public Sub MassReplace()
With Application.FileSearch
.LookIn = "C:\" ' where to search
.SearchSubFolders = True ' search the subfolders
.FileName = "*.doc" ' file pattern to match
' if more than one match, execute the following code
If .Execute() > 0 Then
' 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 the address
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.Text = "OldAddress"
.MatchCase = True
.Replacement.Text = "NewAddress"
End With
selection.Find.Execute Replace:=wdReplaceAll
' replace e-mail address
With selection.Find
.Text = "Oldemail"
.Replacement.Text = "Newemail"
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
address in all the documents in a folder. However, whenever I try to
run it, Word just goes into hourglass-forever mode, and I have to
ctl-alt-del my way out of the program. I don't know much about macros
and have basically cut-and-pasted this into my normal.dot document
which has a couple of other macros that work. Is there something
obviously wrong with this particular macro, or is it just that, as
usual, I don't know what I'm doing?
Thanks for any guidance. Here's the macro:
Public Sub MassReplace()
With Application.FileSearch
.LookIn = "C:\" ' where to search
.SearchSubFolders = True ' search the subfolders
.FileName = "*.doc" ' file pattern to match
' if more than one match, execute the following code
If .Execute() > 0 Then
' 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 the address
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
With selection.Find
.Text = "OldAddress"
.MatchCase = True
.Replacement.Text = "NewAddress"
End With
selection.Find.Execute Replace:=wdReplaceAll
' replace e-mail address
With selection.Find
.Text = "Oldemail"
.Replacement.Text = "Newemail"
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