Results 1 to 5 of 5

Thread: Search a Userform Listbox for matching text from another Listbox

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Search a Userform Listbox for matching text from another Listbox

    Hi - I have the following Code I found hunting on google which I thought would allow me to search the results listed in ListBox2 based on the Selection in ListBox1.

    The contents of ListBox1 is a list of files in a particular folder which have been truncated so as not to show the folder path. This is due to the long length of the folder names and sub folders.

    I therefore added the full path and file names to ListBox2 (which I intend to hide using ListBox2.Visible = False once the userform is working) so that I could call on this to return the full path and filename of the selected file in ListBox1. The reason for this is that I want to launch the selected file and therefore need the full path along with the filename.

    Here's the code I have so far but it's not working. I'm sure I'm doing something daft - or maybe someone can suggest a better way to acheive what I'm looking for?

    [vba]
    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 Long) _
    As Long

    Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

    Dim strFile As String
    Dim strAction as String
    Dim lngErr As Long

    strFile = FindStringinListControl(ListBox2, ListBox1.Value)
    strAction = "OPEN"
    lngErr = ShellExecute(0, strAction, strFile, "", "", 0)

    End Sub


    Public Function FindStringinListControl(ListControl As Object, _
    ByVal SearchText As String) As Long

    'Input:
    'ListControl: List or ComboBox Object
    'SearchText: String to Search For
    'Returns: ListIndex of Item if found
    'or -1 if not found

    Dim lHwnd As Long
    Dim lMsg As Long
    On Error Resume Next
    lHwnd = ListControl.hWND
    If TypeOf ListControl Is ListBox Then
    lMsg = LB_FINDSTRINGEXACT
    ElseIf TypeOf ListControl Is ComboBox Then
    lMsg = CB_FINDSTRINGEXACT
    Else
    FindStringinListControl = -1
    Exit Function
    End If
    FindStringinListControl = SendMessagebyString _
    (lHwnd, lMsg, -1, SearchText)
    End Function[/vba]

    Thanks for looking!

    Cheers,
    rrenis
    Last edited by rrenis; 05-10-2007 at 06:23 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •