Getting out of memory error when looping through folders and subfolders

RLG_1204

New Member
Joined
Jun 10, 2018
Messages
2
Can someone please help me. I am new to VBA coding and I have been stuck on this code for a while now. I am trying to get connection string information on XLSM files that are located in folders and sub folders of a directory. When I ran the code, everything seemed to run fine, but then I got an "out of memory" error. As I was trying to fix this error, I was also getting a "method rows of object_global failed" run time error. At this point I am not sure how to fix the code. I would appreciate anyone's help this. thank you.

Code:
[SIZE=1]Sub ListFiles()
    Dim sRoot       As String
    Dim oFSO        As Scripting.FileSystemObject
 
    sRoot = Sheet1.Range("A1")
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False
   
    Sheet1.Columns.Range("A3:F10000").ClearContents
    Sheet1.Range("A:F").ColumnWidth = 50
    Sheet1.Range("A3:F5000").RowHeight = 50
   
    Set oFSO = New Scripting.FileSystemObject
    RecurseFolder oFSO, sRoot, True
       
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.DisplayAlerts = True
End Sub
 
Sub RecurseFolder(oFSO As FileSystemObject, sDir As String, IncludeSubFolders As Boolean)
    Dim oFil        As File
    Dim oFld        As Folder
    Dim oSub        As Folder
    Dim iRow        As Long
    Dim Wb          As Workbook
    Dim Cn          As WorkbookConnection
    Dim StrFile     As String
    Dim StrFilePath As String
   
   
    iRow = Cells(Rows.Count, "A").End(xlUp).Row + 1
    Set oFld = oFSO.GetFolder(sDir)
    Set Wb1 = ActiveWorkbook
      
      For Each oFil In oFld.Files
      With oFil
            If oFil.Type = "Microsoft Excel Macro-Enabled Worksheet" Then
            On Error Resume Next
            Set Wb = GetObject(oFil.Path)
            DoEvents
            On Error GoTo 0
               
                With Cn
                    For Each Cn In Wb.Connections
                       If Cn.Type = xlConnectionTypeOLEDB Then
                       Rows(iRow).Range("A1:F1").Value = Array(oFld.Path, oFil.Name, Cn.Name, Cn.OLEDBConnection.SourceDataFile, _
                                                            Cn.OLEDBConnection.Connection, Cn.OLEDBConnection.CommandText)
                       ElseIf Cn.Type = xlConnectionTypeODBC Then
                          Rows(iRow).Range("A1:F1").Value = Array(oFld.Path, oFil, Name, Cn.Name, Cn.ODBCConnection.SourceDataFile, _
                                                            Cn.ODBCConnection.Connection, Cn.ODBCConnection.CommandText)
                       End If
                                       
                    iRow = iRow + 1
                    Next Cn
                 
                End With
                 
                    Wb.Close Saved = True
                    DoEvents
                    Set Wb = Nothing
                 
            End If
      End With
           
      
    Next oFil
 
    If IncludeSubFolders Then
        For Each oSub In oFld.SubFolders
            RecurseFolder oFSO, oSub.Path, True
           
        Next oSub
    End If
   
End Sub[/SIZE]
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,224,828
Messages
6,181,206
Members
453,022
Latest member
RobertV1609

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