C
christophercbrewster via OfficeKB.com
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
--
Christopher Brewster
Lockheed Martin, Eagan MN
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200610/1
(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
--
Christopher Brewster
Lockheed Martin, Eagan MN
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200610/1