GetFolder method does not return Folder object (it returns a string instead)

183 Views Asked by At

I'm working on a set of codes that require GetFolder method to return a Folder object. I've written and debugged the following set of codes with the Microsoft Scripting Runtime activated:

Sub test()

Dim fso As FileSystemObject, fld As folder, fl As file

Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder("D:\test\")

MsgBox VarType(fso)
MsgBox VarType(fso.GetFolder("D:\test\"))

End Sub

However, I get an error code 13 for type mismatch. I would like to assign the Folder object (same as expected behavior in documentation) to the fld variable but it isn't working as expected in the documentation.

As I was troubleshooting this, I've tried running the following set of codes:

Sub test()

Dim fso As FileSystemObject, fld As folder, fl As file

Set fso = CreateObject("Scripting.FileSystemObject")

MsgBox VarType(fso)
MsgBox VarType(fso.GetFolder("D:\test\"))

End Sub

The first MsgBox provided an output of 9, indicating that it is an Object variable. However, the second MsgBox provided an output of 8, indicating that it is a String variable. I've also checked against the documentation and other online sources but no other users have encountered this issue.

Edit: Also attempted additional codes as follows:

    Option Explicit
    Sub test()

    Dim fso As FileSystemObject, fld As folder, fl As file

    Set fso = CreateObject("Scripting.FileSystemObject")

    MsgBox TypeName(fso.GetFolder("D:\"))
    MsgBox VarType(fso)
    MsgBox VarType(fso.GetFolder("D:\"))
    MsgBox fso.FolderExists("D:\")
    Set fld = fso.GetFolder("D:\")

    End Sub

MsgBox order returned "Folder", "9", "8", True in sequence before having an error at Set fld = fso.GetFolder("D:").

3

There are 3 best solutions below

2
hennep On

I think that VarType returns 8 because the default property of the folder object is a string

Sub test()
    Dim fso As FileSystemObject, fld As folder, fl As file, f As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    MsgBox "object f=" & VarType(f)
    Set f = fso.GetFolder("C:\temp\")
    MsgBox "object f=" & VarType(f)
    MsgBox f.DateCreated ' <<< it behaves as an object
    MsgBox "default property:" & f
End Sub
0
Alex Tee On

After multiple attempts to resolve it, the set of codes that do not lead to an error are as follows:

    Sub test()

    Dim fso As Object, fld As Object, fl As Object

    Set fso = CreateObject("Scripting.FileSystemObject")

    MsgBox TypeName(fso.GetFolder("D:\"))
    MsgBox VarType(fso)
    MsgBox VarType(fso.GetFolder("D:\"))
    MsgBox fso.FolderExists("D:\")
    Set fld = fso.GetFolder("D:\")

    End Sub

Credit for actively looking for a solution: @OlleSjögren @CHill60

0
Alan Elston On

TLDR: Change the declaration of your Folder variable from Folder to Scripting.Folder

So, I got this annoying problem only in some workbooks.

Initially, my workaround was to change declaring my Folder variable from Folder to Object. Then I noticed that I did not get the problem if I changed declaring my Folder variable from Folder to Scripting.Folder

Sub MicrosoftScriptingRuntimeObjectGetFolder() '  https://stackoverflow.com/questions/77220774/getfolder-method-does-not-return-folder-object-it-returns-a-string-instead
' Early Binding
Dim FSO As Scripting.FileSystemObject: Set FSO = New Scripting.FileSystemObject          '               https://i.postimg.cc/d1GHPGxJ/Microsoft-Scripting-Runtime-Library.jpg

Dim ObjFolder As Folder
Dim ObjFolderObject As Object
Dim ObjScriptFolder As Scripting.Folder

Debug.Print TypeName(FSO.GetFolder(ThisWorkbook.Path & Application.PathSeparator)) ' This always gives me  Folder
Debug.Print VarType(FSO.GetFolder(ThisWorkbook.Path & Application.PathSeparator)) ' This always gives me  8  for  String  type, whether I then get the type mismatch problem at  FSO.GetFolder(   or not.
 
 Set ObjFolderObject = FSO.GetFolder(ThisWorkbook.Path & Application.PathSeparator) ' This always works
 Set ObjScriptFolder = FSO.GetFolder(ThisWorkbook.Path & Application.PathSeparator) ' This always works
 Set ObjFolder = FSO.GetFolder(ThisWorkbook.Path & Application.PathSeparator)       ' This gives the type mismatch error in some workbooks only. Mess about with your checked Library referrences and you might get rid of the problem
End Sub

Furthermore: If I left my Folder variable declared as Folder, I was able to get rid of the problem by unchecking a reference I had to Microsoft Shell Controls And Automation Library. Rechecking that reference to Microsoft Shell Controls And Automation Library does not always bring the problem. Maybe someone smarter can explain that better, or maybe I can later when I get the problem more often and then investigate further how the problem goes away and comes back, but my explanation for now is that if you use just Folder, then depending on other things, it may declare it as a Microsoft Scripting Runtime Library Folder if you are lucky, and if you are unlucky it might declare if to some other Folder object

Alan