Windows Search "A word or ohrase in the file" API, Search in for a string in a file

Hi guys
Is there an API for the windows search in files?
this includes MS Office files, pdf, txt, ...etc.
How can I send s string to this windows search, with the path, and ask it to do the search?


LVL 27
Who is Participating?
PaulHewsConnect With a Mentor Commented:
There is no API to run the search.  Using ShellExecute, you can launch the search, but passing parameters can only be done with simulating key strokes (as far as I know.)

Simple example follows:
Option Explicit
Private Declare Function ShellExecute Lib "Shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As VbAppWinStyle) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
    ShellExecute 0, "find", "C:\", vbNullString, vbNullString, vbNormalFocus
    SendKeys "{tab}this is my phrase" 'Tab is to move to the next input box.
    Sleep 1000
    SendKeys "{Enter}"
End Sub

Open in new window

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
jjafferrAuthor Commented:
Hi mx

This link is to search for a file,
but I want to search for a string in the file, like:
Window's Search "A word or ohrase in the file".

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to and use offer code ‘EXPERTS’ to get 10% off your first purchase.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
oops ...

Well, maybe you could open the file (once found with that API) ... and then do the search ?  Otherwise, I really don't know - but someone probably will.

jjafferrAuthor Commented:
Hi Paul

The code worked like a charm, thanks :)
just incease the sleep to 2000.

Can we capture the results,
or better yet,
can we make this search as a control in a Form?

jjafferrAuthor Commented:
Thanks Erik, I will look at it in a day or two, looks promissing though :)

Well you got me, How did you manage to call me by my first name? humble..

I looked at the API for shell and I came up with this snippet.

This is based off the idea of saving a .fnd file and passing the path to the .fnd file. I am also sure you could use shellexecute to point to the file itself which would accomplish the same task by filling in the edit boxes. However if ths needs to be something you want to search on the fly then this is prob not the way to go because you need to save the search file first from the search dialog.

Option Explicit


Private Declare Function ShFindFiles Lib "shell32" Alias "#90" ( _
    ByVal pidlRoot As Long, _
    ByVal pidlSavedSearch As Long) As Long
Private Declare Function SHParseDisplayName Lib "shell32.dll" ( _
    ByVal pszName As Long, _
    ByVal pbc As Long, _
    ByRef ppidl As Long, _
    ByVal sfgaoIn As Long, _
    ByRef psfgaoOut As Long) As Long

Private Function FindFileSearch(ByVal fndFile As String) As Long
    Dim lpIDList        As Long
    SHParseDisplayName StrPtr(fndFile), 0, lpIDList, 0, 0
    ShFindFiles 0, lpIDList
End Function

Private Sub Command1_Click()
FindFileSearch ("c:\calc.fnd")
End Sub
jjafferrAuthor Commented:
Hi Erik

You are famous, just google your name :)

Thanks for the code, but since you are famous for classes and wrapers, is it possible to capture/save the search result even in a txt file, so that I can retrieve it to a textbox in the Form?

This would be the ideal solution for me.
.fnd saves the search critera and path, but not the results.

The following works for me with Windows Search 4.0

Dim con As ADODB.Connection
Dim rst As ADODB.Recordset

Set con = New ADODB.Connection
Set rst = New ADODB.Recordset
Dim sConString As String
Dim sSQLString As String

sConString = "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"
con.Open sConString
'The following example searches for documents containing the word "software", and the phrase "computer".
sSQLString = "SELECT System.ItemPathDisplay FROM SYSTEMINDEX WHERE FREETEXT(*,'warcraft ""wow""')"

Set rst = con.Execute(sSQLString)

Do Until (rst.EOF)
   Debug.Print rst.Fields.Item("System.ItemPathDisplay").Value

Set rst = Nothing

Set con = Nothing
jjafferrAuthor Commented:

what does Windows Search 4.0 require?
I am now on winXp home edition.

I added a reference to Microsoft ActiveX Data Objects 2.8 ( for the above snippet)

You may need to update your index locations which can be located in
Control Panel :::: Indexing Options
jjafferrAuthor Commented:

Thanks for the info and the code,
but I cannot make the users install additional software on their PC's,
my laptop is up to date with windows updates, but it doesn't have Windows Search 4.0, so it doesn't even get downloaded as part of windows update.

I was hoping to use the existing windows search facility, which every user would have by default.

I am sure you have tried your best, so did Paul,
so I guess it is only fair to close this Q, sharing the points between both of you guys.

Thank you Paul, Erik,
it was nice of you to share your knowledge :)

It's interesting that MS has started making the Windows search accessible through code.  Does the windows search include PDF files and Office files?
jjafferrAuthor Commented:
Yes, both PDF and Office files are included,
there is something called iFilters , which specifies the file types, so if your pc doesn't search for certain files, add the iFilter for that particular file.

I wasn't very clear... I meant does it allow searching for a string inside those types of files.
jjafferrAuthor Commented:
Yup, provided the pdf file is searchable,
and my earlier post stands, for the iFilters.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.