我的VBScript代码出了什么问题?
当它在cmd中运行时,在浏览文件后,“(null):未指定的错误”发生
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
set path=BrowseForFile()
If IsObject( path ) Then
WScript.Echo "Object: ", path
else
WScript.Echo "No object selected; Cancel clicked"
End If
答案 0 :(得分:0)
当我运行代码时,我得到一个不同的错误
test.vbs(32, 1) Microsoft VBScript runtime error: Object required: '[string: " Title: Desktop Path"]'
对于该错误,您只需将set path=BrowseForFile()
更改为更改path=BrowseForFile()
,因为它返回的字符串不是对象。
答案 1 :(得分:0)
看起来好像是在混合测试对象响应和字符串响应。
BrowseForFile函数将返回字符串" Title: " & file.title + " Path: " & file.self.path
,但您的代码set path=BrowseForFile()
期望函数的响应是对象,而不是字符串。
您可以执行path=BrowseForFile()
并处理字符串响应,也可以执行
set 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
测试输出
D:\>cscript test.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.
Object: Title: Desktop Path: C:\Users\SCOTTSUPER\Desktop
答案 2 :(得分:0)
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 IsObject( path ) Then
WScript.Echo "Object: ", path
else
WScript.Echo "No object selected; Cancel clicked"
End If