8

I am trying to read a file into an array list and then return it from a function. My function works to read the file to an array but when I try setting it to a variable it errors saying invalid procedure call or argument

My read file code

function readfile(strFile)
dim fs,objTextFile 
set fs=CreateObject("Scripting.FileSystemObject")
dim arrStr
set objTextFile = fs.OpenTextFile(strFile)
Set userArrayList = CreateObject( "System.Collections.ArrayList" )

Do Until objTextFile.AtEndOfStream 
strNextLine = objTextFile.Readline 
userArrayList.add strNextLine
Loop 

objTextFile.Close
set objTextFile = Nothing
set fs = Nothing

readfile = userArrayList
end function

Calling it in my code

arr = readfile("\\dc1\NETLOGON\Scripts\Add_Users\user.csv")

For Each present In arr
user = split(present,",")
WScript.Echo user(0) & user(1) & user(2) & user(3) & user(4) & "|"
Next

What am I doing wrong?

4

1 回答 1

9

Turns out I need to use set here is the working code. Working function

function readfile(strFile)
dim fs,objTextFile 
set fs=CreateObject("Scripting.FileSystemObject")
dim arrStr
set objTextFile = fs.OpenTextFile(strFile)
Set userArrayList = CreateObject( "System.Collections.ArrayList" )

Do Until objTextFile.AtEndOfStream 
strNextLine = objTextFile.Readline 
userArrayList.add strNextLine
Loop 

objTextFile.Close
set objTextFile = Nothing
set fs = Nothing

set readfile = userArrayList
end function

Working Call

Dim arr
set arr = readfile("\\dc1\NETLOGON\Scripts\Add_Users\user.csv")
For Each present In arr
user = split(present,",")
WScript.Echo user(0) & user(1) & user(2) & user(3) & user(4) & "|"
'WScript.Echo present & "|"
Next
于 2012-10-24T16:46:25.557 回答