Accessing the Windows Search from VB

Anyone have a code sample of calling the windows search function from VB?

This would be the same as clicking START/FIND/FILES and FOLDERS, typing in the search criteria and clicking the SEARCH button.

Basically I want the user to be able to type in some text in a box and when they click search, my program would launch the search function as if the user did all the steps listed above. I would provide the start path and pass along the information the user typed in.

The other solution would be for me to just walk down the directory tree myself in the code but since I don't actually do anything after the search I am thinking calling the windows search would be a better solution.
LVL 3
schworakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richie_SimonettiIT OperationsCommented:
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can open a Find Files window by using this code but I don't know about passing information to it.  (Possibly DDE here?)

dim objShell

set objShell = CreateObject("Shell.Application")
objShell.FindFiles
set objShell = nothing
0
Richie_SimonettiIT OperationsCommented:
" (Possibly DDE here?)"
Hummmm.... it should be a problem since w98 and w2k/xp/millenium have different ways to show search dialog. Optionally, it could be do with sendkeys but is a mess and error prone.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

schworakAuthor Commented:
I have a recursive search block that I can use but it isn't really what I want to use for this project. But if I can't get the built in search running via code then I will do it.


The shell object opened the search window but I don't see a way to pass parameters and start the search. Any clues?
0
fantasy1001Commented:
This will open the window search:

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const KEYEVENTF_EXTENDEDKEY = &H1
Const KEYEVENTF_KEYUP = &H2

Private Sub Command1_Click()
    keybd_event 91, 0, 0, 0
    keybd_event 70, 0, 0, 0
    keybd_event 70, 0, KEYEVENTF_KEYUP, 0
    keybd_event 91, 0, KEYEVENTF_KEYUP, 0
End Sub

Will come back later to insert the parameter. Thanks & Cheers
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fantasy1001Commented:
This program shows how to search for a filename by specifying a directory from which to begin the search.

Create a new project in Visual Basic. Form1 is created by default.
Add the following code to the General Declarations section of Form1:
    Dim XFilename As String
    Dim StrtPath As String

Create a new procedure called Get_Files. Add the following code to this procedure:
Private Sub Get_Files(FPath As String)
    Dim File_Name As String
    Dim File_Path As String
    Dim File_Read As Integer
    Dim X As Boolean
    Dim I As Integer

    File_Path = FPath & "\"
    File_Name = Dir$(File_Path, vbDirectory)
    File_Read = 1
    X = False

    Do While File_Name <> ""
        If File_Name <> "." And File_Name <> ".." Then
            If GetAttr(File_Path & File_Name) = vbDirectory Then
                StrtPath = File_Path & File_Name
                List1.AddItem StrtPath
                X = True
                Get_Files StrtPath
            End If
        End If
        If X = True Then
            File_Name = Dir$(File_Path, vbDirectory)
            For I = 2 To File_Read
                File_Name = Dir$
            Next
            X = False
        End If
        File_Name = Dir$
        File_Read = File_Read + 1
    Loop
End Sub

Create a new procedure called List_Files. Add the following code to this procedure:
Private Sub List_Files()
    Dim XIndex As Integer
    Dim XName As String
    Dim X_Filename As String

    For XIndex = 0 To List1.ListCount - 1
        XName = List1.List(XIndex) & "\" & XFilename
        X_Filename = Dir(XName)
        If X_Filename <> "" Then
            List2.AddItem List1.List(XIndex) & "\" & X_Filename
                Do While True
                    X_Filename = Dir
                    On Error GoTo exit_loop
                        If X_Filename <> "" Then
                            List2.AddItem List1.List(XIndex) & "\" & X_Filename
                        Else
                            Exit Do
                        End If
                Loop
        End If
exit_loop:
        Next XIndex
End Sub

Add a Label control to Form1. Label1 is created by default. Set its Caption property to "Directory".
Add a Text Box control to Form1. Text1 is created by default. Set its Text property to an empty string.
Add a second Label control to Form1. Label2 is created by default. Set its Caption property to "Filename".
Add a second Text Box control to Form1. Text2 is created by default. Set its Text property to an empty string.
Add a List Box control to Form1. List1 is created by default. Set its Visible property to False.
Add a second List Box control to Form1. List2 is created by default.
Add a Command Button control to Form1. Command1 is created by default. Set its Caption property to "Search".
Add the following code to the Click event for Command1:
Private Sub Command1_Click()
    Dim XLen As Integer
   
    XLen = Len(Text1.Text)
   
    If Mid$(Text1.Text, XLen, 1) = "\" Then
        StrtPath = Left$(Text1.Text, XLen - 1)
    Else
        StrtPath = Text1.Text
    End If
   
    XFilename = Text2.Text
   
    List1.Clear
    List2.Clear
   
    List1.AddItem StrtPath
    Get_Files StrtPath
    List_Files
   
    If List2.ListCount = 0 Then
        MsgBox "Unable to find file"
    Else
        MsgBox "Search completed"
    End If
End Sub

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.