Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Accessing the Windows Search from VB

Posted on 2003-10-22
7
265 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 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