OK, getting desperate enough to drop this one in (then continue to try to resolve it, until some wit highlights my schoolboy mistake).
I have been a member for 17 years though!
I have a function that basically returns a list of files found in any directory (folder).
It worked fine whilst using an array. I decided to switch to a collection (arrays were getting on my nerves).
The function should return a collection.
(And as I type that, I had an inkling that maybe I should PASS a collection to it, as, the collection is actually being created WITHIN the function! Hmmm).
Anyway, I've started so I'll finish. Here's the function (the compiler baulks at 'ListFilesDir = col')...
If you get to it before me - you get 2 stars.
After = 1.
Thanks,
Darrylle
I have been a member for 17 years though!
I have a function that basically returns a list of files found in any directory (folder).
It worked fine whilst using an array. I decided to switch to a collection (arrays were getting on my nerves).
The function should return a collection.
(And as I type that, I had an inkling that maybe I should PASS a collection to it, as, the collection is actually being created WITHIN the function! Hmmm).
Anyway, I've started so I'll finish. Here's the function (the compiler baulks at 'ListFilesDir = col')...
Code:
Public Function ListFilesInDir(strPath As String, _
Optional strFilter As String = "*") As Collection
Dim col As Collection
Dim strFile As String
On Error GoTo ErrHandle
' Make sure path has terminating \ ...
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
strFile = Dir(strPath & "*." & strFilter) ' Add file extension.
Set col = New Collection
' Loop through DIR files...
Do While strFile <> vbNullString
If strFile <> "." And strFile <> ".." Then
col.Add strFile ' Add found file to collection.
End If
strFile = Dir 'Next file in DIR.
Loop
ListFilesInDir = col ' <----------------Error here.
col.Clear
ErrExit:
On Error Resume Next
Exit Function
ErrHandle:
MsgBox "Error Number: " & Err.Number & vbCrLf & _
"Error Source: ListFilesInDir" & vbCrLf & _
"Error Description: " & Err.Description
Resume ErrExit
End Function
If you get to it before me - you get 2 stars.
After = 1.
Thanks,
Darrylle