从 Excel VBA 调用 .vbs 脚本
问题描述:
是否可以在需要时从 VBA 代码调用 .vbs 脚本?如果可能的话,你能给我一个如何做到这一点的示例代码吗?
Is it possible to call a .vbs script from a VBA code whenever needed? If possible then can you give me a sample code of how to do so?
答
运行文件:
Shell "wscript c:\null\a.vbs", vbNormalFocus
如果 VBS 想要使用控制台,请将 wscript
替换为 cscript
.
replacing wscript
with cscript
if the VBS wants to use the console.
或者您可以添加对Microsoft Script Control 并直接与 VBScript 运行时交互以执行 VBS 代码、程序等;
Or you can add a reference to the Microsoft Script Control and interact with the VBScript runtime directly to execute VBS code, procedures etc;
Dim scr As ScriptControl: Set scr = New ScriptControl
scr.Language = "VBScript"
scr.AddCode "sub T: msgbox ""All Hail Cthulhu"": end sub"
scr.Run "T"