We have old Excel VBA function that creates an array of the files that are in a network folder. The files are then used in a report compiler to open and copy file contents into a big report. I am having an issue in getting the function to read the fold contents.
This was written by a former employee and we have just migrated from Excel 2007 to Excel 2013. I have trusted the document, location and added the VBA Analysis Toolpak - VBA Add in. So the code is running. My issue is the it does not read the files in the folder for some reason. I believe it's deriving the correct folder file path. Does anyone have any suggestions as I've stepped through it many times now and have no idea why it would not find anything.
If I bypass the "If Not UBound(ary) = 0 Then" line at the end of the function, I can get the code to find 1 of the files (the last one), but I'm still missing all 44 of the others.
Any help is appreciated.
This was written by a former employee and we have just migrated from Excel 2007 to Excel 2013. I have trusted the document, location and added the VBA Analysis Toolpak - VBA Add in. So the code is running. My issue is the it does not read the files in the folder for some reason. I believe it's deriving the correct folder file path. Does anyone have any suggestions as I've stepped through it many times now and have no idea why it would not find anything.
If I bypass the "If Not UBound(ary) = 0 Then" line at the end of the function, I can get the code to find 1 of the files (the last one), but I'm still missing all 44 of the others.
Any help is appreciated.
Code:
Function FilesInFolder(FolderOrFiles As Variant) As Boolean
Dim FSO As Object ' FileSystemObject
Dim fsoFOL As Object ' Folder
Dim fsoFILE As Object ' File
Dim ary
ReDim ary(0 To 0)
Set FSO = CreateObject("Scripting.FileSystemObject")
Set fsoFOL = FSO.GetFolder(FolderOrFiles)
For Each fsoFILE In fsoFOL.Files
If Not fsoFILE.Path = ThisWorkbook.FullName Then
'If fsoFILE.Type = "Microsoft Office Excel Worksheet" _
'And Not fsoFILE.Path = ThisWorkbook.FullName Then
ReDim Preserve ary(1 To UBound(ary) + 1)
ary(UBound(ary)) = fsoFILE.Path
End If
Next
If Not UBound(ary) = 0 Then
FilesInFolder = True
FolderOrFiles = ary
End If
End Function