Solved

Accessing the Windows Search from VB

Posted on 2003-10-22
7
260 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:schworak
7 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9602473
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9602529
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9602576
" (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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:schworak
ID: 9602874
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
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9604386
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
 
LVL 7

Accepted Solution

by:
_agj_ earned 250 total points
ID: 9604580
0
 
LVL 5

Expert Comment

by:fantasy1001
ID: 9604744
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now