VBscript运行时错误

VBscript运行时错误,vbscript,Vbscript,我有一个VBScript,我正在建设工作。脚本本身完成了其中的所有函数——只有在脚本结束后才会抛出错误。它给了我以下错误: vbscript运行时错误:需要对象“objFSO” 以下是相关函数: Function ReadFileIntoArray (sFile) dim objFSO 'As FileSystemObject dim file dim volumes() Set file = objFSO.OpenTextFile(sFile) 'Error

我有一个VBScript,我正在建设工作。脚本本身完成了其中的所有函数——只有在脚本结束后才会抛出错误。它给了我以下错误: vbscript运行时错误:需要对象“objFSO”

以下是相关函数:

Function ReadFileIntoArray (sFile)
    dim objFSO 'As FileSystemObject
    dim file
    dim volumes()

    Set file = objFSO.OpenTextFile(sFile)  'Error Thrown Here.
    do while not file.AtendOfStream
        redim preserve text(nlines)
        volumes(nlines) = file.Readline
        nlines = nlines + 1
    loop

    file.close
    set file = nothing
    Set objFSO = nothing

    ReadFileIntoArray = volumes
end Function 

该文件仍被打开并正确使用。我有点迷路了。有什么想法吗?

objFSO
从未被赋值。发生错误时,
objFSO
的值为
,它不是对象

你可能失踪了

   Set objFSO = CreateObject("Scripting.FileSystemObject")

objFSO
从未分配值。发生错误时,
objFSO
的值为
,它不是对象

你可能失踪了

   Set objFSO = CreateObject("Scripting.FileSystemObject")

@Nutsy:@Thom Smith有答案,我只是认为您可能还需要明确设置打开文件的方式。我已更新了您的代码,以包含一些
常量

Function ReadFileIntoArray (sFile)
    dim objFSO 'As FileSystemObject
    dim file
    dim volumes()

    Const ForAppending = 8
    Const ForReading   = 1
    Const ForWriting   = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")        
    Set file = objFSO.OpenTextFile(sFile, ForReading)  'Error Thrown Here.
    do while not file.AtendOfStream
        redim preserve text(nlines)
        volumes(nlines) = file.Readline
        nlines = nlines + 1
    loop

    file.close
    set file = nothing
    Set objFSO = nothing

    ReadFileIntoArray = volumes
end Function

脚本中可能还有一些其他问题,我不确定
redim preserve text(nlines)
会做什么,因为您再也不会在任何地方定义或使用
text
,也不会将
nlines
定义或初始化为
0

@Nutsy:@Thom Smith有答案,但我认为您可能还需要明确设置打开文件的方式。我已更新了您的代码,以包含一些
常量

Function ReadFileIntoArray (sFile)
    dim objFSO 'As FileSystemObject
    dim file
    dim volumes()

    Const ForAppending = 8
    Const ForReading   = 1
    Const ForWriting   = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")        
    Set file = objFSO.OpenTextFile(sFile, ForReading)  'Error Thrown Here.
    do while not file.AtendOfStream
        redim preserve text(nlines)
        volumes(nlines) = file.Readline
        nlines = nlines + 1
    loop

    file.close
    set file = nothing
    Set objFSO = nothing

    ReadFileIntoArray = volumes
end Function

脚本中可能还有一些其他问题,我不确定
redim preserve text(nlines)
会做什么,因为您再也不会在任何地方定义或使用
text
,也不会将
nlines
定义或初始化为
0

如果您使用类似于:

Set objFSO = CreateObject("Scripting.FileSystemObject") 

strVar = ReadFileIntoArray(File)
它进入函数后,您无需重新分配objFSO,只需写入:

Function ReadFileIntoArray (sFile)

dim file
dim volumes()

Set file = objFSO.OpenTextFile(sFile)  'Error Thrown Here.
do while not file.AtendOfStream
    redim preserve text(nlines)
    volumes(nlines) = file.Readline
    nlines = nlines + 1
loop

file.close
set file = nothing
Set objFSO = nothing

ReadFileIntoArray = volumes
end Function

如果您使用的是类似于:

Set objFSO = CreateObject("Scripting.FileSystemObject") 

strVar = ReadFileIntoArray(File)
它进入函数后,您无需重新分配objFSO,只需写入:

Function ReadFileIntoArray (sFile)

dim file
dim volumes()

Set file = objFSO.OpenTextFile(sFile)  'Error Thrown Here.
do while not file.AtendOfStream
    redim preserve text(nlines)
    volumes(nlines) = file.Readline
    nlines = nlines + 1
loop

file.close
set file = nothing
Set objFSO = nothing

ReadFileIntoArray = volumes
end Function

感谢您的回复-我现在将测试它,并让您知道结果!感谢您的回复-我现在将测试它,并让您知道结果!