Help with FileSearchObject, please?

E

Ed

I'm trying to write a macro based on the FileSearchObject. I'm trying to do
the same thing as File>Open>Tools>Find. I will present an Input Box to the
user, asking for File Name and Text String search criteria, then use these
strings in the macro. I pulled the following code from the Help file for
the FileSearchObject, but it needs some tweaking:

Set fs = Application.FileSearch
With fs
.LookIn = "C:\My Documents"
.FileName = "*.doc"
If .Execute(SortBy:=msoSortbyFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
MsgBox "There were " & .FoundFiles.Count & _
" file(s) found."
For i = 1 To .FoundFiles.Count
MsgBox .FoundFiles(i)
Next i
Else
MsgBox "There were no files found."
End If
End WithWhere, and how, do I insert the text strings to search for? Also,
this code presents the names of all the found files in message boxes, one at
a time. If there's a zillion files meeting the criteria, I get a zillion
messages. How can I get a single list of all the file names?Thanks.Ed
 
M

Malcolm Smith

Ed

What if you had something like:

Dim sFoundFiles as string

For i = 1 To .FoundFiles.Count
sFoundFiles = sFoundFiles & .FoundFiles(i) & vbcrlf
Next i

MsgBox sFoundFiles


Malc
www.dragondrop.com
 
E

Ed

Thank you, Malcolm. That worked well - except when I tested it, the number
of files found were more than could be shown in the message box that came
up! I think it showed 12 file names, then stopped. Is there a way to
create a scrolling box? Or list the file names some other way? There could
easily be hundreds of file names returned.

Ed
 
M

Malcolm Smith

Ah, now we're talking about creating a user form.

Okay, create a form called frmFileList and with a control called
lstFilesFound then what you do in your code is this:



dim ofrmFileList as frmListList

' Other bits of code here

If .Execute(SortBy:=msoSortbyFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
set ofrmFileList = New frmFileList

For i = 1 To .FoundFiles.Count
frmFileList.lstFilesFound.AddItem .FoundFiles(i)
next i
ofrmFileList.Show vbModal
'------------------------
unload ofrmFileList
set oFrmFileList = nothing
endif


and in the cmdOK button of the form have the following code:

Me.Hide


And Robert, as they say, is your father's brother.

Hope that this helps and if you need an explanation then please do shout.

Malc
www.dragondrop.com
 
E

Ed

Malcolm, I do thank you for your help. I've never worked with User Forms
before, but I guess it's time to learn!

I hope I am not stretching your patience when I ask for a bit more help on
the other question I asked in my original post. The File>Open>Tools>Find
dialog allows the user to specify text strings within the documents to be
ssearched; it will then return only documents containing thse text strings.
In the following code to use to search for my files, where do I specify
those text strings?

With Application.FileSearch
.NewSearch
' Set primary folder to search.
.LookIn = "C:\My Documents"
' Search sub folders of primary search folder.
.SearchSubFolders = True
' Search for only Word documents.
.FileName = "*.doc"
.MatchAllWordForms = True
.FileType = msoFileTypeWordDocuments

Thank you for all your help.

Ed

PS - I'm not sure your reference to my relative translated fully across the
pond?!?
 
M

Malcolm Smith

Ed

There is a tutorial on my site which may help you with forms for the first
time.

I'll get back to your other question tomorrow, if I may, as it's late and
I need sleep.

Malc
www.dragondrop.com
 
M

Malcolm Smith

Ed

To be honest I am not sure. I would imagine that you would have to open
every document you discover and then search the contents.

There perhaps is an easier way; perhaps an API which you could use. The
reason why I haven't looked at this for years is that I tend to work with
clients who have a document management system and so that would have a
full text indexer inside.

There is something which gnaws inside my head which tells me that there is
a file search API or something. Gosh this is most annoying; but somewhere
in the depths of my mind a bell is tolling...

Can someone else shed light on this?

Malc
 

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

Similar Threads


Top