Return result from Python to Vba
<p>I'm using a VBA code that calls a python script. I can send a parameter to my python script and reading it using <code>sys.argv[1]</code>.</p>

<p>In the python code I have a function that takes the given argument and return a value.</p>

<p>Please how can I get the return value in VBA? </p>

<p>Consider using VBA Shell's <code>StdOut</code> to capture a stream of the output lines. Be sure to have the Python script print to screen the value:</p>

<p><strong>Python</strong></p>

<pre><code>...
print(outputval)
</code></pre>

<p><strong>VBA</strong> <em>(<code>s</code> below would be string output)</em></p>

<pre class="lang-vb prettyprint-override"><code>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""" &amp; " " &amp; arg

    Set oExec = oShell.Exec(oCmd)
    Set oOutput = oExec.StdOut

    While Not oOutput.AtEndOfStream
        sLine = oOutput.ReadLine
        If sLine &lt;&gt; "" Then s = s &amp; sLine &amp; vbNewLine
    Wend

    Debug.Print s

    Set oOutput = Nothing: Set oExec = Nothing
    Set oShell = Nothing

End Sub
</code></pre>

<p><strong>Credit</strong></p>

<p>Script borrowed from <a href="http://stackoverflow.com/users/243392/bburns-km">@bburns.km</a>, non-accepted answer, from this <a href="http://stackoverflow.com/a/32600510/1422451">SO post</a></p>

python vba