0

这是我经常使用的子程序的摘录:

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

为什么我不能myFolder更具体地声明而不是变体 - 或者我可以吗?我试过将它声明为一个集合,但它出错了......

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
4

1 回答 1

2

设置对微软脚本运行时的引用并将其声明为Dim myFolder As Scripting.Folder

编辑

要获取文件集合,请使用脚本运行时库中的 files 集合。一个例子如下所示。

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 :如果要将 myFolder 标注为文件集合而不是文件夹,则可以通过以下方式进行:

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

如果人们期望它是一个文件夹而不是一个文件集合,这可能会有点令人困惑,因此重命名它可能会更好。

于 2013-08-25T12:02:25.823 回答