Multi-file find: select files; open read-only (NEED HELP)

  • Thread starter christophercbrewster via OfficeKB.com
  • Start date
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
 
C

Cindy M.

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
:)
 
C

christophercbrewster via OfficeKB.com

Thanks for the information. When you say "this is being removed", what
specifically-- the FileSearch method?

Chris Brewster
Hi Christophercbrewster,


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.


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
:)

--
Christopher Brewster
Lockheed Martin, Eagan MN

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/200610/1
 
C

Cindy M.

Hi Christophercbrewster,
Thanks for the information. When you say "this is being removed", what
specifically-- the FileSearch method?
Yes.

Cindy Meister
 

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