2015-09-23 26 views
1

我的VBScript代碼有什麼問題?我的VBScript代碼有什麼問題? (null):未指定的錯誤消息

當在cmd中運行時,它將瀏覽該文件後, 「(空):未指定的錯誤」 發生

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

它看起來好像是混合測試對象響應和字符串響應。

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 
+0

是的,省略了設置。但仍然有這個錯誤! – rey

+0

我想我們都需要知道你想從BrowseForFile函數中得到什麼。你想要你構造的字符串還是diaglog提示符中的對象? 你可以發佈整個代碼,因爲你已經修改它? – ScottEB

+0

我需要的字符串,路徑..我只是省略了「設置」.. – rey

0

當我運行代碼我得到不同的錯誤

test.vbs(32, 1) Microsoft VBScript runtime error: Object required: '[string: " Title: Desktop Path"]'

對於該錯誤,你只需要更改set path=BrowseForFile()更改path=BrowseForFile(),因爲它返回的字符串不是對象。

+0

我變了,但還是有一個錯誤VBS(7,2)(空):未指定的錯誤 – rey

+0

沒有說什麼你行上的錯誤? – Robert

+0

它只是這樣說的:(7,2)(null):未指定的錯誤..你知道(7,2)是什麼意思嗎? – rey

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 
+0

你應該編輯你的問題。而且你也想改變'If IsObject(path)Then'如果TypeName(path)=「String」那麼' – Robert

+0

好吧,謝謝你提及!是的,改變了,仍然有一個錯誤.. – rey