将参数从 VBA 发送到 VBS,然后再次将 argument/variable 从 VBS 拉到 VBA
Send arguments from VBA to VBS then again pull an argument/variable from VBS to VBA
我正在使用 Wscript.Shell 从 VBA 执行 VBS 文件,并将一些参数传递给 VBS。现在,我希望 Wscript.Shell 等待来自 VBS 的 arguments/variable 的 return。任何线索,我该如何实现?谢谢
VBA代码为:
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代码:
Dim Return_VBS_Val
'something to do here
Msgbox Wscript.Arguments(0)
Return_VBS_Val = "Hello VBA"
使用 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 文件:
Msgbox Wscript.Arguments(0) 'read input
Wscript.Echo "Hello VBA" 'pass output
我正在使用 Wscript.Shell 从 VBA 执行 VBS 文件,并将一些参数传递给 VBS。现在,我希望 Wscript.Shell 等待来自 VBS 的 arguments/variable 的 return。任何线索,我该如何实现?谢谢
VBA代码为:
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代码:
Dim Return_VBS_Val
'something to do here
Msgbox Wscript.Arguments(0)
Return_VBS_Val = "Hello VBA"
使用 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 文件:
Msgbox Wscript.Arguments(0) 'read input
Wscript.Echo "Hello VBA" 'pass output