-----Original Message-----
Hilary,
Following is some code I wrote to find specific words or phrases in a whole
directory of Word docs. The Windows search interface allows you to search
for files "Containing text ...", but this won't work for Word documents that
are Protected For Forms, and the majority of documents I needed to search in
were online forms that were protected in this way, so I wrote this macro to
do the search. I believe it could be easily adapted for your needs.
Sub FindFormsWithSpecifiedText()
Dim fs As New Scripting.FileSystemObject
Dim tFolder As Folder, aFile As File, testDoc As Document
Dim strAdd As String, strText As String
Documents.Add
strText = InputBox("Text to find in folder path:")
WordBasic.DisableAutoMacros
Set tFolder = fs.GetFolder([supply a folder path here])
For Each aFile In tFolder.Files
Set testDoc = Documents.Open(aFile.Path)
On Error Resume Next
If Not testDoc.ProtectionType = wdNoProtection Then _
testDoc.Unprotect [supply a standard password here]
If testDoc.ProtectionType = wdNoProtection Then
With Selection.Find
.ClearFormatting
.Text = strText
.Wrap = wdFindContinue
End With
Selection.Find.Execute
If Selection.Find.Found Then
strAdd = testDoc.Name & vbCr
Else
strAdd = ""
End If
Else
strAdd = testDoc.Name & "could not be unprotected with standard
" & _
"password. No search performed." & vbCr
End If
testDoc.Close wdDoNotSaveChanges
Selection.TypeText strAdd
Next aFile
End Sub
The macro tests each document in the specified folder for protection. If
the document is protected, the macro attempts to unprotect it using a
standard password that you specify in the code. If it is able to unprotect
the document with that password, it searches for the specified text. It
generates a report in a new blank doc that lists all the files which
contained the specified text, as well as any files that couldn't be
unprotected with the standard password. Mine was set up to generate a
report because I had to use a change control process to modify each form,
but you could simply do a replace for each Find.Found. Also, all the
documents I needed to search in were located in a specific Workgroup
Templates location, so I hard-coded that into my procedure, but you could
also use something like:
Dim oShell as Object
Dim oFolder as Object
Set oShell = CreateObject("Shell.Application")
Set oFolder = oShell.BrowseForFolder(0, "Select a Folder", 0, ssfDrives)
This would allow the user to browse for a specific folder whose files they
want to search.
Hope this is enough to get you started. Regards,
Chad DeMeyer
Hilary Ratner said:
Hi.
I have a user with hundreds of individual Word docs. She
wants to open each doc and replace a phrase with another
phrase and save as a new doc.
Rather than open, search and replace, and save each doc
individually, is there a way to make this process faster?
My solutions were:
1. Create a master document containing the individual docs
and then do a global search and replace. (I haven't had
much luck with master documents in the past so I am a
little scared to try that.)
2. Use macros. Is it possible to create a macro that would
open, search and replace, and save as?
Is there an easier way? Any thoughts are appreciated!
Thanks!
Hilary
.