2017-09-14 96 views

回答

-1

本示例調用函數或子函數並動態地找到正確的調用。在命令行中,您提供'functionToExecute'作爲您想調用的函數的名稱或'subToExecute'作爲您想調用的子例程。對於從命令行調用VBS一分兩個例子分別是功能:

cscript.exe demo.vbs subToExecute 
cscript.exe demo.vbs functionToExecute 

在VBS腳本得到命令行參數的程序的名稱,並決定是否要調用一個子程序或致電功能:用於VBS命令行處理

On Error Resume Next 
mySubOrFuncName = WScript.Arguments.Item(0) 'name of the subroutine or function 
subCall = "call " & mySubOrFuncName & "()" 
Execute subCall 

If Err.Number <> 0 Then 'if sub could not be called, take the function call 
    eval(mySubOrFuncName) 
    Err.Clear 
End If 

WScript.Quit 

'this is a subroutine you already have 
Sub subToExecute 
    MsgBox "inside a sub" 
    'do stuff 
End Sub 

'this is a function you already have 
Function functionToExecute 
    MsgBox "inside a function" 
    'do stuff 
End Function 

簡便的事例:https://ss64.com/vb/syntax-args.html

+0

這幫助了我。但正如你所提到的,eval()不適合我。 – user8533698

+2

@ user8533698'Eval()'評估它不能執行完整語句的條件,您應該使用'Execute()'/'ExecuteGlobal()'。另外,上面的例子會因爲分配給'a'的字符串不正確而導致語法錯誤。不要接受不完整/錯誤的答案,它不會幫助任何人。 – Lankymart

+1

剛剛糾正了賦值爲'a'的語法錯誤 –