1

I'm writing a vbscript function that looks something like this:

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

As you can see I'm creating an ArrayList and then adding all the names of excel files that exist in a specified folder.

I then call this function and use the Set operator to specify that I'm expecting an object to be returned.

Set XLSFileList = fnGetXLSFileCount

When I check the count on the object it seems to be correct. When I try to pull the names out there is nothing there. What am I doing incorrectly here?

For each file in XLSFileList
    name = file.Item(0)
Next
1
  • Yes, the () is optional there. Commented Mar 30, 2016 at 17:23

1 Answer 1

1

The For Each loop already enumerates the items of the collection. And since you assign just the names to the collection you simply use the loop variable to get the name:

For Each file In XLSFileList
    name = file
Next

The Item property can be used to directly access a specific item from the collection:

WScript.Echo XLSFileList.Item(0)
Sign up to request clarification or add additional context in comments.

1 Comment

I feel so stupid. I was just over complicating it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.