我正在尝试自动化Outlook桌面应用。我在vb脚本文件(file.vbs)中编写了不同的子过程,它们在Outlook上执行不同类型的操作。我想从命令行调用其中一个子过程。我该怎么做?
答案 0 :(得分:0)
此示例调用function或sub并动态查找正确的调用。在命令行中,您提供'functionToExecute'作为您要调用的函数的名称,或者'subToExecute'作为您要调用的子例程。从命令行分别为子和函数调用VBS的两个示例:
cscript.exe demo.vbs subToExecute
cscript.exe demo.vbs functionToExecute
在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
VBS命令行处理的简单示例:https://ss64.com/vb/syntax-args.html