How to solve the error User defined type not defined?

t.mo picture t.mo · Aug 28, 2017 · Viewed 7.4k times · Source

Below is a procedure to search folder for a folder like "requirements". When beginning to step through I receive an error message because of FileSystemObject:

User defined type not defined

What am I missing?

Sub GetSubFolders()

Dim f As Folder, sf As Folder, myFile As File
Dim fso As New FileSystemObject                   ' <=========== ERROR HERE

Set f = fso.GetFolder("C:\Users\C58227\Desktop\Projects\CRDs")

For Each sf In f.SubFolders
    For Each mySubFolder In myFolder.SubFolders
        For Each myFile In mySubFolder.Files
            If myFile.Name Like "Requirements" Then
                MsgBox myFile.Name
                Exit For
            End If
        Next

        MsgBox "Else"
    Next
Next

End Sub

Answer

Shai Rado picture Shai Rado · Aug 28, 2017

You are trying to use FileSystemObject (early binding) without specifying the reference to it in the Project References.

You need to add Microsoft Scripting Runtime reference as can be seen in the screen-shot below:

enter image description here