+ Reply to Thread
Results 1 to 5 of 5

Code to set combo box to list folders in a directory

Hybrid View

  1. #1
    Registered User
    Join Date
    09-19-2011
    Location
    Birmingham
    MS-Off Ver
    Excel 2003 - 07
    Posts
    69

    Code to set combo box to list folders in a directory

    Hi All pretty self explanetry im trying to write some code (for outlook but should be the same) using a userform to show a list of folders then opens the selected folder but ive got a mind blank anyone that can help would be epic!!!

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code to set combo box to list folders in a directory

    Maybe:
    Option Explicit
    Private Const strBasePath As String = "C:\Users\Public\"
    Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
        Shell "explorer.exe " & Me.ListBox1.value, vbNormalFocus
    End Sub
    
    Private Sub UserForm_Initialize()
    
    Dim objFSO              As Object
    Dim objFolderCollection As Object
    Dim objFolder           As Object
    Dim varFolders          As Variant
    Dim lngCounter          As Long
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolderCollection = objFSO.getfolder(strBasePath).subfolders
    
    ReDim varFolders(0 To objFolderCollection.Count - 1, 0 To 1)
    
    For Each objFolder In objFolderCollection
        varFolders(lngCounter, 0) = objFolder.Path
        varFolders(lngCounter, 1) = objFolder.Name
        lngCounter = lngCounter + 1
    Next objFolder
    
    With Me.ListBox1
        .ColumnCount = 2
        .ColumnWidths = "0,100"
        .List = varFolders
    End With
    
    End Sub

  3. #3
    Registered User
    Join Date
    09-19-2011
    Location
    Birmingham
    MS-Off Ver
    Excel 2003 - 07
    Posts
    69

    Re: Code to set combo box to list folders in a directory

    my good friend you are a legend you have no idea how long ive been searching for a solution to this!!!! for time saving if i wanted say 4 different list boxes on the same input form would i just change all the "listbox1" parts to 2 ext and the file path? if not its no problem thanks in advance

    Edit: and the "Strbasepath" to a 2 3 ect
    Last edited by Kezwick; 08-08-2013 at 05:04 AM.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Code to set combo box to list folders in a directory

    Thanks for the feedback, glad it helped. Are you wanting all the listboxes to display the same list of files or different ones?

    Maybe:
    Option Explicit
    Private objFSO As Object
    
    Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
        Shell "explorer.exe " & Me.ListBox1.value, vbNormalFocus
    End Sub
    Private Sub ListBox2_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
        Shell "explorer.exe " & Me.ListBox2.value, vbNormalFocus
    End Sub
    Private Sub ListBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
        Shell "explorer.exe " & Me.ListBox3.value, vbNormalFocus
    End Sub
    
    Private Sub UserForm_Initialize()
    
    Dim objControl As MSForms.Control
    
    For Each objControl In Me.Controls
        If TypeOf objControl Is MSForms.ListBox Then
            With objControl
                .ColumnCount = 2
                .ColumnWidths = "0,100"
            End With
        End If
    Next objControl
    
    Me.ListBox1.List = GetFolderList("C:\Users\Public\")
    Me.ListBox2.List = GetFolderList("C:\Some Folder\")
    Me.ListBox3.List = GetFolderList("C:\Some other Folder\")
    
    End Sub
    
    Private Function GetFolderList(strPath As String) As Variant
    
        Dim objFSO              As Object
        Dim objFolderCollection As Object
        Dim objFolder           As Object
        Dim varFolders          As Variant
        Dim lngCounter          As Long
        
        If objFSO Is Nothing Then Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set objFolderCollection = objFSO.getfolder(strPath).subfolders
        
        ReDim varFolders(0 To objFolderCollection.Count - 1, 0 To 1)
        
        For Each objFolder In objFolderCollection
            varFolders(lngCounter, 0) = objFolder.Path
            varFolders(lngCounter, 1) = objFolder.Name
            lngCounter = lngCounter + 1
        Next objFolder
    
        GetFolderList = varFolders
        
    End Function
    Last edited by Kyle123; 08-08-2013 at 06:29 AM.

  5. #5
    Registered User
    Join Date
    09-19-2011
    Location
    Birmingham
    MS-Off Ver
    Excel 2003 - 07
    Posts
    69

    Re: Code to set combo box to list folders in a directory

    its no bigy just me bing lazy realy haha the solution you provided was brill so i jsut coppied it over 3 userforms and it works a dream!! many thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA code to open folders and pdf file inside the folders
    By kirtesh250187 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2013, 03:17 AM
  2. Replies: 0
    Last Post: 12-04-2012, 01:01 PM
  3. Found Code to List All Folders and Size in Certain Directory. Need Help Editing.
    By jcranst in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-09-2012, 03:11 PM
  4. Generate a list of folders within a directory
    By TRT7G7 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-17-2010, 11:24 AM
  5. [SOLVED] List of folders in a certain directory
    By woolyhedgehog in forum Excel General
    Replies: 1
    Last Post: 10-19-2005, 12:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1