【VBA】Run VBScript file

You can run “VBScript file“! 

Use “Run method” of “WScript.Shell object“!

PR

VBA Code

For example, run following “VBScript file“:
*Show message.

MsgBox("Run VBScript file!")

'Normal return code
WScript.Quit(0)

'Error return code
'WScript.Quit(1)
VBScript file
VBScript file

You can run “VBScript file” by “Run method” of “WScript.Shell object“.

Option Explicit

Sub sampleProc()

    Dim vbsFile As String
    Dim wsh As Object
    Dim result As Integer
    
    'Set "VBScript file path"
    vbsFile = "C:\Users\user\Desktop\sample.vbs"
    
    Set wsh = CreateObject("WScript.Shell")
    
    'Run the "VBScript file"
    'Return
    '┗0 : Succeeded
    '┗1 : failed
    result = wsh.Run(vbsFile, WaitOnReturn:=True)
    
    If (result = 0) Then
        MsgBox ("Succeeded")
    Else
        MsgBox ("failed ")
    End If
    
    Set wsh = Nothing
    
End Sub

Set “VBScript file path“(Line 10).

Run the “VBScript file“(Line 18).

If you want wait until the “VBScript file” finishes, set “WaitOnReturn” to “True“(Line 18).
*If not, set “WaitOnReturn” to “False”.

PR

Result

You ran “VBScript file“!

result①
result①
result②
result②
タイトルとURLをコピーしました