Calling a .vbs script from Excel VBA

后端 未结 4 1947
眼角桃花
眼角桃花 2020-12-08 08:43

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?

4条回答
  •  萌比男神i
    2020-12-08 09:25

    To run a file:

    Shell "wscript c:\null\a.vbs", vbNormalFocus
    

    replacing wscript with cscript if the VBS wants to use the console.

    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"
    

提交回复
热议问题