场景:我试图从excel中的vba代码运行python脚本。代码运行时没有错误,但不会产生任何结果。
VBA中的代码:
Private Sub CommandButton1_Click()
Dim Ret_Val
Dim args As String
args = "\\X\s\D\D\Data_manager.py"
Ret_Val = Shell("C:\Users\D\AppData\Local\Continuum\anaconda2\python.exe" & " " & args, vbNormalFocus)
'also tried this, same result
Ret_Val = Shell("C:\Users\D\AppData\Local\Continuum\anaconda2\python.exe \\X\s\D\D\Data_manager.py")
End Sub
问题:几个月前我运行了相同的代码并且它运行得很好,但现在,由于某种原因,它表现得非常奇怪。关于可能发生的事情的任何想法?
OBS1:我也读过SO中的其他帖子有类似的问题,但对于那些,VBA似乎抛出一个错误,这是我的代码中不会发生的事情。
OBS2:我知道代码没有产生任何结果,因为我的python脚本非常简单,它要求输入几个输出并将它们输出到csv文件。
应该是这样的。
RetVal = Shell("<full path to python.exe> " & "<full path to your python script>")
或者如果python脚本与工作簿位于同一文件夹中,那么您可以尝试:
RetVal = Shell("<full path to python.exe> " & ActiveWorkBook.Path & "\<python script name>")
How to call python script on excel vba?
或者,试试这个:
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
wsh.Run "cmd.exe /S /C perl a.pl c:\temp", windowStyle, waitOnReturn