Arup Rakshit
Arup Rakshit

Reputation: 118289

Calling a .vbs script from Excel VBA

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?

Upvotes: 15

Views: 57266

Answers (4)

Sebastiaan35
Sebastiaan35

Reputation: 31

You may want to use """ if your path name contains a - For me the following solved this issue:

Shell "cscript """ & ActiveWorkbook.Path & """\your.vbs", vbNormalFocus

Upvotes: 0

user7022675
user7022675

Reputation: 1

Try somthing like this

ChDir ThisWorkbook.Path
Shell "wscript " & ThisWorkbook.Path & "\your.vbs", vbNormalFocus

It helped me.

Upvotes: 0

Alex K.
Alex K.

Reputation: 175936

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"

Upvotes: 27

Jarekczek
Jarekczek

Reputation: 7896

I only want to add to Alex' answer, that in some environments the object must be created in the following way:

set scr = CreateObject("MSScriptControl.ScriptControl")

If Alex adds this to his answer, I will delete this one.

Upvotes: 10

Related Questions