Mark LaGrange
asked on
FileSystemObject getting "type mismatch" error
Hello - the Debug.print statements below get what you'd expect, but the "Set fldr..." statement errors out with a "type mismatch" error.
This code used to work!
What's wrong?
Thanks
Public Function LoopFilesInFolder() As Integer
Dim fso As New FileSystemObject, fldr As Folder, fyle As File
Debug.Print fso.GetFolder("C:\RPA\").F iles.Count
Debug.Print fso.GetFolder("C:\RPA\")
Set fldr = fso.GetFolder("C:\RPA\")
End Function
This code used to work!
What's wrong?
Thanks
Public Function LoopFilesInFolder() As Integer
Dim fso As New FileSystemObject, fldr As Folder, fyle As File
Debug.Print fso.GetFolder("C:\RPA\").F
Debug.Print fso.GetFolder("C:\RPA\")
Set fldr = fso.GetFolder("C:\RPA\")
End Function
ASKER
well, I had to add the "Set fldr = " to the GetFolder()
With fso
Set fldr = .GetFolder("C:\RPA\")
End With
but it still got the "type mismatch" error.
I should mention that this code works fine on my personal laptop (1), but the error is happening on my work pc (2)
(1) 64-bit, W7, Office 360, Reference set for MS Scripting Runtime, C:\Windows\SysWOW64\scrrun .dll)
(2) 32-bit, W7, Office 2010, Reference set for MS Scripting Runtime, C:\Windows\system32\scrrun .dll)
With fso
Set fldr = .GetFolder("C:\RPA\")
End With
but it still got the "type mismatch" error.
I should mention that this code works fine on my personal laptop (1), but the error is happening on my work pc (2)
(1) 64-bit, W7, Office 360, Reference set for MS Scripting Runtime, C:\Windows\SysWOW64\scrrun
(2) 32-bit, W7, Office 2010, Reference set for MS Scripting Runtime, C:\Windows\system32\scrrun
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Humm ...
I just tested on W7 Access 2010 ... no error.
I just happen to be working on some code right now using FSO ...
I just tested on W7 Access 2010 ... no error.
I just happen to be working on some code right now using FSO ...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, it works. Moving the Scripting ref up seems to have fixed it. :-/
Thank you both for your responses.
Thank you both for your responses.
Whatever it takes :-)
If reference order resolved your issue, then you should make sure to disambiguate your references - i.e., write "Dim fld As FileSystemObject.Folder" instead of "Dim fld As Folder". If you had another reference "higher" in the reference list that also included the Folder object, VBA would grab that reference instead of the one you intend, and then when you try to set the <unknown>.fldr object to the FileSystemObject.Folder object, VBA would complain, and you'll get your error.
ASKER
Ok, thanks Scott - I'll make a note of this.
Even better, switch to late binding, and kiss bye bye to references troubles.
ASKER
I just tried juggling the ref's around, and it worked, just a shot in the dark.
I appreciate the responses of the two experts. They have both helped me many times.
I appreciate the responses of the two experts. They have both helped me many times.
With fso
.GetFolder("C:\RPA\")
' more code
End With