Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Excel/VBA运行Python脚本_Python_Excel_Vba - Fatal编程技术网

通过Excel/VBA运行Python脚本

通过Excel/VBA运行Python脚本,python,excel,vba,Python,Excel,Vba,多年来我一直在试图找到解决这个问题的办法。我有一些python脚本需要定期更新,并且希望能够使用宏来实现这一点。现在我双击这些文件,它们通过Shell执行,没有问题 Sub RunPyScript() Dim Ret_Val As Variant Dim command As String command = Chr(34) & "C:\Users\Jon Doe\python.exe" & Chr(34) & " " & Chr(34) & "C:\

多年来我一直在试图找到解决这个问题的办法。我有一些python脚本需要定期更新,并且希望能够使用宏来实现这一点。现在我双击这些文件,它们通过Shell执行,没有问题

Sub RunPyScript()

Dim Ret_Val As Variant
Dim command As String

command = Chr(34) & "C:\Users\Jon Doe\python.exe" & Chr(34) & " " & Chr(34) & "C:\Users\Jon Doe\" & "\Roto.py" & Chr(34)
Ret_Val = Shell(command, vbNormalFocus)

End Sub
当我尝试运行上面的宏时,它看起来会像双击时一样运行,但是Shell在脚本执行之前退出(我认为这是问题所在,不是积极的)。如果有人能帮我,我会非常感激的

Dim objShell
'Dim command             'use this declaration type for VBS (Script), instead of the direct declaration like as "As String"
Dim command as String   'use this declaration type for VBA (Access/Excel)

command = Chr(34) & "C:\Users\John Doe\python.exe" & Chr(34) & " " & Chr(34) & "C:\Users\John Doe\" & "\roto.py" & Chr(34)
Set objShell = CreateObject("WScript.Shell")

objShell.Run command, 1, True

'Settings for WindowStyle:
'     0 Hide the window (and activate another window.)
'     1 Activate and display the window. (restore size and position) Specify this flag when displaying a window for the first time.
'     2 Activate & minimize.
'     3 Activate & maximize.
'     4 Restore. The active window remains active.
'     5 Activate & Restore.
'     6 Minimize & activate the next top-level window in the Z order.
'     7 Minimize. The active window remains active.
'     8 Display the window in its current state. The active window remains active.
'     9 Restore & Activate. Specify this flag when restoring a minimized window.
'    10 Sets the show-state based on the state of the program that started the application.
顺便说一句,您可以简单地使用2x引号将引号插入字符串变量,就像我在以前的源代码中使用的那样。例如,
StrVariable=“”
给出结果
”。因此,命令行的字符串将是:

command=”“%USERPROFILE%\python.exe”“%USERPROFILE%\roto.py”“”

结果如下:

“C:\Users\John Doe\python.exe”“C:\Users\John Doe\roto.py”