2016-09-15 158 views
2

私はPythonスクリプトを呼び出すVBAコードを使用しています。私はPythonスクリプトにパラメータを送り、sys.argv[1]を使ってそれを読むことができます。PythonからVbaへの返り値

私は与えられた引数をとり、値を返す関数を持っています。

VBAで戻り値を取得するにはどうすればよいですか?

答えて

4

VBAシェルのStdOutを使用して、出力行のストリームをキャプチャすることを検討してください。値スクリーニングするためのPythonスクリプトのプリントを持ってしてください:

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 

クレジット(以下の文字列出力になりs

Pythonの

... 
print(outputval) 

VBA

借りたスクリプト@bburns.km、これからの返答、このSO post

関連する問題