Home > Blockchain >  Send arguments from VBA to VBS then again pull an argument/variable from VBS to VBA [duplicate]
Send arguments from VBA to VBS then again pull an argument/variable from VBS to VBA [duplicate]

Time:09-29

I am executing VBS files from VBA using Wscript.Shell and passing some arguments to VBS. Now, I want the Wscript.Shell wait for the return of the arguments/variable from VBS. Any lead, how do I achieve this? Thanks

VBA code is:

Sub LaunchScript()

    Dim wsh As Object
    Dim waitOnReturn As Boolean: waitOnReturn = True
    Dim windowStyle As Integer: windowStyle = 1 
    Dim scriptPath As String, MyMsg As String
    Dim Return_VBS_Val
    
    scriptPath = "C:\path\x.vbs"
    MyMsg = "I got into VBS"
     
     Set wsh = VBA.CreateObject("WScript.Shell")
     wsh.Run """" & scriptPath & """ """ & MyMsg & """", windowStyle, waitOnReturn
    
    'Return_VBS_Val = argument from x.vbs  >>>>> I want to pull the argument from VBS here and store it in this VBA Variable

End sub

x.vbs code:

Dim Return_VBS_Val 

'something to do here

Msgbox Wscript.Arguments(0)

Return_VBS_Val = "Hello VBA" 

CodePudding user response:

Example using StdOut:

Sub LaunchScript()

    Dim scriptPath As String, MyMsg As String
    
    scriptPath = "C:\path\x.vbs"
    MyMsg = "I got into VBS"
     
    Debug.Print "Ouput: " & ExecShellCmd("cscript.exe """ & _
                         scriptPath & """ """ & MyMsg & """")
    
End Sub

Public Function ExecShellCmd(FuncExec As String) As String
    ExecShellCmd = VBA.CreateObject("WScript.Shell") _
                    .exec("cmd.exe /c " & FuncExec).stdout.readall
End Function

VBS file:

Msgbox  Wscript.Arguments(0) 'read input
Wscript.Echo "Hello VBA"   'pass output
  • Related