I'm trying to adapt code from O'Reilly's "Word Hacks"
(FindReplaceAllDocsInFolder, #31). Instead of replacing the string, I need to
identify and list which files contain a given string. The changes I've tried
have failed:
* The "open" should be read-only because the files are under the control of
an application. [Work-around: I can close that app but would rather not.]
* Files whose names start with "~" should be ignored. Same work-around.
* Show-stopper, no workaround: The search itself does not find the string,
even when the code correctly opens a file.
* I need to list files the contain the string. Don't know if my code works
for this.
The code with comments is below. Any help is appreciated.
Chris Brewster
___________________________________________________
Public Sub MultiFileFind()
Dim i As Integer
Dim doc As Document
Dim rng As Range
With Application.FileSearch
.NewSearch
.LookIn = "C:\OutputDir"
.SearchSubFolders = False
' *** The following doesn't work. Want to ignore "~Name.doc" files
.Name = not("~*")
.FileType = msoFileTypeWordDocuments
If Not .Execute() = 0 Then
For i = 1 To .FoundFiles.Count
' *** Need to add ".Readonly" to this Open but can't make it work:
Set doc = Documents.Open(.FoundFiles(i))
Set rng = doc.Range
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
' .Text = "Search string goes here"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' *** The following line fails to find the string:
.Execute FindText = "String to find goes here"
' *** Running from a document, need to record name of file containing
string.
' *** Don't know if this works because haven't found string yet.
Selection.TypeText Text:=.FoundFiles(i).Name
Selection.TypeParagraph
End With
doc.Close
Set rng = Nothing
Set doc = Nothing
Next i
Else
MsgBox "No files matched " & .FileName
End If
End With
End Sub

Signature
Christopher Brewster
Lockheed Martin, Eagan MN
Cindy M. - 05 Oct 2006 15:36 GMT
Hi Christophercbrewster,
> With Application.FileSearch
I don't know if it makes a difference to you, but...
I understand this is being removed from the Office 2007
object models. It might make more sense to use the old
"Dir" function if your solution needs to work in future
versions.
> .Name = not("~*")
Well, looking at the FileSearch object model, the property
is FileName, not Name.
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 17 2005)
http://www.word.mvps.org
This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:-)
christophercbrewster - 05 Oct 2006 22:19 GMT
Thanks for the information. When you say "this is being removed", what
specifically-- the FileSearch method?
Chris Brewster
>Hi Christophercbrewster,
>
[quoted text clipped - 21 lines]
>follow question or reply in the newsgroup and not by e-mail
>:-)

Signature
Christopher Brewster
Lockheed Martin, Eagan MN
Cindy M. - 06 Oct 2006 13:32 GMT
Hi Christophercbrewster,
> Thanks for the information. When you say "this is being removed", what
> specifically-- the FileSearch method?
Yes.
Cindy Meister