2016-03-30 31 views
1

我正在寫一個VBScript函數,它看起來是這樣的:列出在VBScript中返回的對象

Public Function fnGetXLSFileCount() 
Dim fso, src, folder, file, fileList 

    Set fileList = CreateObject("System.Collections.ArrayList") 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    src = "\\myserver\myfolder" 
    Set folder = fso.GetFolder(src) 
    For Each file In folder.files 
     If LCase(fso.GetExtensionName(file)) = "xlsx" Then 
      fileList.Add file.name 
     End If 
    Next 
    Set fnGetXLSFileCount = fileList 

End Function 

正如你可以看到我創建一個ArrayList,然後加入存在於Excel的文件,所有名字指定的文件夾。

然後我調用這個函數,並使用Set運算符來指定我期待的對象被返回。

Set XLSFileList = fnGetXLSFileCount 

當我檢查對象的計數,它似乎是正確的。 當我試圖拉出名字時,那裏什麼也沒有。我在這裏做錯了什麼?

For each file in XLSFileList 
    name = file.Item(0) 
Next 
+0

是的,()在那裏是可選的。 –

回答

1

For Each循環已經枚舉集合的項目。既然你指定只是將名稱和集合只需使用循環變量來獲取名稱:

For Each file In XLSFileList 
    name = file 
Next 

Item屬性可用於直接訪問從收集的特定項目:

WScript.Echo XLSFileList.Item(0) 
+0

我覺得很愚蠢。我剛剛結束了它。謝謝。 –