1

Here's the VBA code I'm using in MS Access (from Microsoft's support site - no edits):

**

Private Sub Command3_Click()
         Dim fso As New FileSystemObject
         Dim f As Folder, sf As Folder, path As String
         'Initialize path.
         path = Environ("windir")
         'Get a reference to the Folder object.
         Set f = fso.GetFolder(path)
         'Iterate through subfolders.
         For Each sf In f.SubFolders
           Debug.Print sf.Name
         Next
      End Sub

**

I have also created the directory "C:\Test" and tried path = "C:\Test\" and path = "C:\Test" I am aware that the reference "Microsoft Scripting Runtime" needs to be enabled - and it is.

The code compiles without error. Despite the above, I'm still getting Run-time Error '13' Type Mismatch when I try to run it.

Is there anything else I'm doing wrong?

braX
  • 11,506
  • 5
  • 20
  • 33
XB-70
  • 11
  • 1

1 Answers1

0

Try late binding

      Private Sub Command3_Click()
       Dim fso As Object
     Set fso = CreateObject("Scripting.FileSystemObject")

      Dim f As Object, sf As Object, path As String
     'Initialize path.
     path = Environ("windir")
     'Get a reference to the Folder object.
     Set f = fso.GetFolder(path)
     'Iterate through subfolders.
     For Each sf In f.SubFolders
       Debug.Print sf.Name
     Next
  End Sub
xShen
  • 452
  • 4
  • 9
  • Thanks. I tried late binding. Now, I'm getting Error '91 - Object Variable or With Block Variable not set. I did some looking around to find out why and I'm stumped. I also tested with path = "C:\Test\" and "C:\Test" I'm still getting the same error. – XB-70 Apr 27 '20 at 13:11
  • It looks like the scripting library is not registered on your system. So try to follow the answer on https://stackoverflow.com/questions/56396335/failure-to-set-fso-createobject-with-exceltoword-add-in – xShen Apr 28 '20 at 05:02