Solved

Accessing the Windows Search from VB

Posted on 2003-10-22
7
259 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

708 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

16 Experts available now in Live!

Get 1:1 Help Now