How to capture subfolder names with VB6

Posted on 2007-10-14
Last Modified: 2012-06-22
I'm trying to create a program that will capture subfolder names in a given folder.  I have the following code:

Option Explicit
Private mfso As Scripting.FileSystemObject

Sub main()
    Dim objFolder As Scripting.Folder
    For Each objFolder In mfso.GetFolder("C:\test").SubFolders
        MsgBox "folder found"
End Sub

Now I know this won't capture the name, but I'm trying to figure out how I want to proceed.  I thought this code could help me find each subfolder that exists in C:\test.  When I run this code, I get the following error:

Run-time error '91':
Object variable or With block variable not set

Does anyone know what I'm doing wrong?
Question by:bpl5000
    LVL 92

    Accepted Solution

    Hello bpl5000,

    You probably forgot to instantiate the FileSystemObject.

    Sub main()
        Dim objFolder As Scripting.Folder
        Set mfso = New Scripting.FileSystemObject

        For Each objFolder In mfso.GetFolder("C:\test").SubFolders
            MsgBox "folder found"

        Set objFolder = Nothing
        Set mfso = Nothing

    End Sub


    LVL 5

    Author Comment

    D'oh!  How could I miss that?
    Thanks Patrick!

    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…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    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…

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now