我正在使用一个调用python脚本的VBA代码。我可以将参数发送到python脚本并使用读取sys.argv[1]。
sys.argv[1]
在python代码中,我有一个函数,该函数接受给定的参数并返回一个值。
请问如何在VBA中获得返回值?
考虑使用VBA ShellStdOut捕获输出线流。确保打印Python脚本以筛选值:
StdOut
蟒蛇
... print(outputval)
VBA (s下面是字符串输出)
s
Public Sub PythonOutput() Dim oShell As Object, oCmd As String Dim oExec As Object, oOutput As Object Dim arg As Variant Dim s As String, sLine As String Set oShell = CreateObject("WScript.Shell") arg = "somevalue" oCmd = "python ""C:\Path\To\Python\Script.py""" & " " & arg Set oExec = oShell.Exec(oCmd) Set oOutput = oExec.StdOut While Not oOutput.AtEndOfStream sLine = oOutput.ReadLine If sLine <> "" Then s = s & sLine & vbNewLine Wend Debug.Print s Set oOutput = Nothing: Set oExec = Nothing Set oShell = Nothing End Sub