Reputation: 35557
This is an extract of a sub-routine I use lot:
Private Sub DeleteFilesNotCreatedToday(myTargetFolder As String)
Dim myFolder
Dim myFile
Dim YesterdayDate As Date
YesterdayDate = Date
Set myFolder = CreateObject("Scripting.FileSystemObject").GetFolder(myTargetFolder).Files
For Each myFile In myFolder
...
...
Next myFile
Why can't I declare myFolder
more specifically instead of variant - or can I? I've tried declaring it as a collection but it errors...
Private Sub DeleteFilesNotCreatedToday(myTargetFolder As String)
Dim myFolder As Collection
Dim myFile
Dim YesterdayDate As Date
YesterdayDate = Date
Set myFolder = CreateObject("Scripting.FileSystemObject").GetFolder(myTargetFolder).Files
For Each myFile In myFolder
...
...
Next myFile
Upvotes: 0
Views: 152
Reputation: 1239
Set a reference to microsoft scripting runtime and declare it as Dim myFolder As Scripting.Folder
Edit
To get a collection of files use the files collection in the scripting runtime library. An example is shown below.
Sub CollectFiles()
Dim myFile As Scripting.File
Dim myFolder As scripting.folder
Set myFolder = CreateObject("Scripting.FileSystemObject").GetFolder("C:\")
For Each myFile In myFolder.Files
Next myFile
End Sub
Edit2 : If you want to dimension myFolder as a collection of files rather than a folder you can do it the following way:
Sub CollectFiles()
Dim myFile As Scripting.File
Dim myFolder As Scripting.Files
Set myFolder = CreateObject("Scripting.FileSystemObject").GetFolder("C:\").Files
For Each myFile In myFolder
Next myFile
End Sub
It might get a bit confusing if people expect it to be a folder rather than a files collection so it would probably better to rename it.
Upvotes: 2