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:").
I think that VarType returns 8 because the default property of the folder object is a string