什么';我的VBScript代码有什么问题?(null):未指定的错误消息

什么';我的VBScript代码有什么问题?(null):未指定的错误消息,vbscript,Vbscript,我的VBScript代码有什么问题 当它在cmd中运行时,在浏览文件之后,会发生“(null):未指定的错误” 代码如下: Dim Firstresponse Dim Secondresponse Dim path Firstresponse = inputbox("ID") if IsEmpty(Firstresponse) Then 'cancel button was pressed WScript.Quit End If Secondresponse = input

我的VBScript代码有什么问题

当它在cmd中运行时,在浏览文件之后,会发生“(null):未指定的错误”

代码如下:

Dim Firstresponse
Dim Secondresponse
Dim path

Firstresponse = inputbox("ID")

if IsEmpty(Firstresponse) Then 
   'cancel button was pressed
    WScript.Quit
End If

Secondresponse = inputbox("File Dir")

if IsEmpty(Secondresponse) Then 
   'cancel button was pressed
   WScript.Quit
End if

set path=BrowseForFile()
If IsObject( path ) Then
    WScript.Echo "Object: ", path
else
    WScript.Echo "No object selected; Cancel clicked"
End If

当我运行代码时,我得到一个不同的错误

test.vbs(32, 1) Microsoft VBScript runtime error: Object required: '[string: " Title: Desktop Path"]' test.vbs(32,1)Microsoft VBScript运行时错误:需要对象:“[string:“Title:Desktop Path”]”
对于该错误,您只需更改
set path=BrowseForFile()
即可更改
path=BrowseForFile()
,因为它返回的是字符串而不是对象。

运行代码时,会出现不同的错误

test.vbs(32, 1) Microsoft VBScript runtime error: Object required: '[string: " Title: Desktop Path"]' test.vbs(32,1)Microsoft VBScript运行时错误:需要对象:“[string:“Title:Desktop Path”]”
对于该错误,您只需将
set path=BrowseForFile()
更改为
path=BrowseForFile()
,因为它返回的是一个字符串而不是一个对象。

看起来您正在混合测试对象响应和字符串响应

BrowseForFile函数将返回字符串
“Title:”&file.Title+“Path:”&file.self.Path
,但是您的代码
set Path=BrowseForFile()
希望函数的响应是对象,而不是字符串

您可以执行
path=BrowseForFile()
并处理字符串响应,也可以执行

设置path=BrowseForFile()
并修改函数以返回对象:

Function BrowseForFile()
    Dim shell 
    Dim file 
    Set shell = CreateObject("Shell.Application")
    set file = shell.BrowseForFolder(0, "Please choose a file", &h4001& ,OpenAt)

    If Not file is Nothing Then
        set BrowseForFile = file
    Else 
        WScript.Quit
    End If
End Function
这里有一个完整的测试文件要尝试。我注释掉了输入框,所以这只会提示选择文件夹并回显您构建的字符串

Function BrowseForFile()
    Dim shell 
    Dim file 
    Set shell = CreateObject("Shell.Application")
    set file = shell.BrowseForFolder(0, "Please choose a file", &h4001&, OpenAt)

    If Not file is Nothing Then
       BrowseForFile = " Title: " & file.title + " Path: " & file.self.path
    Else 
        WScript.Quit
    End If
End Function

Dim Firstresponse
Dim Secondresponse
Dim path

'Firstresponse = inputbox("ID")
'if IsEmpty(Firstresponse) Then 
   'cancel button was pressed
'    WScript.Quit
'End If

'Secondresponse = inputbox("File Dir")
'if IsEmpty(Secondresponse) Then 
   'cancel button was pressed
'   WScript.Quit
'End if

path=BrowseForFile()
If path <> "" Then
    WScript.Echo "Object: ", path
else
    WScript.Echo "No object selected; Cancel clicked"
End If

看起来您正在混合测试对象响应和字符串响应

BrowseForFile函数将返回字符串
“Title:”&file.Title+“Path:”&file.self.Path
,但是您的代码
set Path=BrowseForFile()
希望函数的响应是对象,而不是字符串

您可以执行
path=BrowseForFile()
并处理字符串响应,也可以执行

设置path=BrowseForFile()
并修改函数以返回对象:

Function BrowseForFile()
    Dim shell 
    Dim file 
    Set shell = CreateObject("Shell.Application")
    set file = shell.BrowseForFolder(0, "Please choose a file", &h4001& ,OpenAt)

    If Not file is Nothing Then
        set BrowseForFile = file
    Else 
        WScript.Quit
    End If
End Function
这里有一个完整的测试文件要尝试。我注释掉了输入框,所以这只会提示选择文件夹并回显您构建的字符串

Function BrowseForFile()
    Dim shell 
    Dim file 
    Set shell = CreateObject("Shell.Application")
    set file = shell.BrowseForFolder(0, "Please choose a file", &h4001&, OpenAt)

    If Not file is Nothing Then
       BrowseForFile = " Title: " & file.title + " Path: " & file.self.path
    Else 
        WScript.Quit
    End If
End Function

Dim Firstresponse
Dim Secondresponse
Dim path

'Firstresponse = inputbox("ID")
'if IsEmpty(Firstresponse) Then 
   'cancel button was pressed
'    WScript.Quit
'End If

'Secondresponse = inputbox("File Dir")
'if IsEmpty(Secondresponse) Then 
   'cancel button was pressed
'   WScript.Quit
'End if

path=BrowseForFile()
If path <> "" Then
    WScript.Echo "Object: ", path
else
    WScript.Echo "No object selected; Cancel clicked"
End If


我改变了,但是仍然有一个错误vbs(7,2)(null):未指定的错误它只是说:(7,2)(null):未指定的错误。。你知道(7,2)是什么意思吗?它的意思是第7行,我明白了,但是第7行应该没有任何问题如果是空的(Firstresponse),那么您可以更新您的问题,将所有代码放在一个代码块中,与vbs文件中的代码完全相同。我这样问是因为你发布的代码在你修复问题时起作用,我是@ScottEB指出的,所以我认为我们看到的和你脚本中的内容是不同的。它可能很小但很重要。我更改了,但仍然有一个错误vbs(7,2)(null):未指定的错误它只是说:(7,2)(null):未指定的错误。。你知道(7,2)是什么意思吗?它的意思是第7行,我明白了,但是第7行应该没有任何问题如果是空的(Firstresponse),那么您可以更新您的问题,将所有代码放在一个代码块中,与vbs文件中的代码完全相同。我这样问是因为你发布的代码在你修复问题时起作用,我是@ScottEB指出的,所以我认为我们看到的和你脚本中的内容是不同的。它可能很小但很重要。我想我们都需要知道你想要从BrowseForFile函数中得到什么。您想要构造的字符串还是diaglog提示符中的对象?您能在修改后发布整个代码吗?我用完整的VBS代码示例编辑了我的答案,该示例打印了BrowseForFolder中的字符串。也许OpenAt应该设置为0或36(系统)。谢谢,这样可以工作。在你发表评论之前,我就知道为什么它不起作用了。问题是我没有填充文本框,然后“如果IsEmpty(Secondresponse)那么WScript.Quit”正在运行。我不知道Secondresponse会返回您在文本框中填充的字符串,它与cancel按钮没有任何关系。我想我们都需要知道您想要从BrowseForFile函数中得到什么。您想要构造的字符串还是diaglog提示符中的对象?您能在修改后发布整个代码吗?我用完整的VBS代码示例编辑了我的答案,该示例打印了BrowseForFolder中的字符串。也许OpenAt应该设置为0或36(系统)。谢谢,这样可以工作。在你发表评论之前,我就知道为什么它不起作用了。问题是我没有填充文本框,然后“如果IsEmpty(Secondresponse)那么WScript.Quit”正在运行。我不知道Secondresponse返回您在文本框中填充的字符串,它与cancel按钮无关。您应该编辑您的问题。您还需要将“If IsObject(path)”更改为“If TypeName(path)=“String”,然后“您应该编辑您的问题”。您还需要将'If IsObject(path)'更改为'If TypeName(path)=“String”Then'