File System Object Late Binding

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This code:

Code:
Private Sub LoopFolders(ByRef Loc As String)
    
    Dim fso As Scripting.FileSystemObject
    Set fso = New Scripting.FileSystemObject
    
    Dim fol As Scripting.Folder
    
    Dim f As Scripting.File
    
    Dim subfol As Scripting.Folder
    
    Set fol = fso.GetFolder(FolderPath:=Loc)
    
    For Each f In fol.Files
        
        Call GetInfo(f:=f)
       
    Next f
    
    For Each subfol In fol.SubFolders
        
        Loc subfol.Path
    
    Next subfol

Private Sub GetInfo(ByRef f As Scripting.File)

    Call Workbooks.Open(Filename:=f.Path)

End Sub

was adapted from here:

Code:
https://www.wiseowl.co.uk/vba-macros/videos/vba-files-folders/files-folders/

I want to convert it late binding.

I've tried this:

Code:
Private Sub LoopFolders(ByRef Loc As String)
    
    Dim fso As Object 'Scripting.FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Dim fol As Object
    
    Dim f As Object
    
    Dim subfol As Object
    
    Set fol = fso.GetFolder(FolderPath:=Loc)
    
    For Each f In fol.Files
        
        Call GetInfo(f:=f)

    Next f
 
    For Each subfol In fol.SubFolders
        
        LoopFolders subfol.Path
    
    Next subfol

End Sub

Private Sub GetInfo(ByRef f)

    Call Workbooks.Open(Filename:=f.Path)

End Sub

but it fails on this line:

Code:
For Each f In fol.Files

How can I convert it to late binding?

Thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
This is a routine for iterating through folders and subfolders with late binding.

VBA Code:
Dim oFSO As Object, ar(2000, 0), x As Long

Sub jec()
 Set oFSO = CreateObject("Scripting.FileSystemObject")
 With Application.FileDialog(4)
   If .Show = 0 Then Exit Sub
   GetFiles .SelectedItems(1)
   If x Then Cells(2, 2).Resize(x) = ar
   Erase ar: x = 0
 End With
End Sub

Sub GetFiles(xFold)
 Dim Obj As Object
 If Right(xFold, 1) <> "\" Then xFold = xFold & "\"
 With oFSO.GetFolder(xFold)
    For Each Obj In .Files
       ar(x, 0) = Obj.Name
       x = x + 1
    Next
    For Each Obj In .SubFolders
       GetFiles xFold & Obj.Name
    Next
 End With
End Sub
 
Upvote 0
This is a routine for iterating through folders and subfolders with late binding.

VBA Code:
Dim oFSO As Object, ar(2000, 0), x As Long

Sub jec()
 Set oFSO = CreateObject("Scripting.FileSystemObject")
 With Application.FileDialog(4)
   If .Show = 0 Then Exit Sub
   GetFiles .SelectedItems(1)
   If x Then Cells(2, 2).Resize(x) = ar
   Erase ar: x = 0
 End With
End Sub

Sub GetFiles(xFold)
 Dim Obj As Object
 If Right(xFold, 1) <> "\" Then xFold = xFold & "\"
 With oFSO.GetFolder(xFold)
    For Each Obj In .Files
       ar(x, 0) = Obj.Name
       x = x + 1
    Next
    For Each Obj In .SubFolders
       GetFiles xFold & Obj.Name
    Next
 End With
End Sub
Thanks for the code.
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,611
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top