Link to home
Start Free TrialLog in
Avatar of Mark LaGrange
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\").Files.Count
    Debug.Print fso.GetFolder("C:\RPA\")
   
    Set fldr = fso.GetFolder("C:\RPA\")
   
End Function
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

How about ..

With fso

    .GetFolder("C:\RPA\")
' more code

End With
Avatar of Mark LaGrange
Mark LaGrange

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)
SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Humm ...
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, it works. Moving the Scripting ref up seems to have fixed it.  :-/
Thank you both for your responses.
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.
Ok, thanks Scott - I'll make a note of this.
Even better, switch to late binding, and kiss bye bye to references troubles.
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.