Add Subfolders into List

Posted on 2006-05-17
Last Modified: 2010-05-01

I have a textbox, list, and command button on my form.
I want it so when you press the command button, whatever directory in the textbox e.g. C:\Documents and Settings\All Users\ I want all the folders and sub folders to come up in the list box as follows:

C:\Documents and Settings\All Users\Local Settings\
C:\Documents and Settings\All Users\Documents\
C:\Documents and Settings\All Users\Documents\My Music\

and so on....
How do i do this?

Question by:iamatramp
    1 Comment
    LVL 76

    Accepted Solution

    Greetings, iamatramp.

    Something like this will do it.  

    Dim objFSO As Object

    Private Sub Command1_Click()
        LoadFolders Text1.Text
    End Sub

    Sub LoadFolders(strPath As String)
        Dim objRootFolder As Object, _
            objFolder As Object
        Set objRootFolder = objFSO.GetFolder(strPath)
        For Each objFolder In objRootFolder.SubFolders
            List1.AddItem objFolder.Path
            LoadFolders objFolder.Path
    End Sub

    Private Sub Form_Load()
        Set objFSO = CreateObject("Scripting.FileSystemObject")
    End Sub


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    745 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

    15 Experts available now in Live!

    Get 1:1 Help Now