从VBA运行并执行python脚本

时间:2022-05-19 01:02:17

I'm trying to run a python script from my VBA module. I've tried pretty much every example I've seen on the internet and so for have had no luck. In the VBA module I also run a .bat file, and it works perfectly:

我正在尝试从我的VBA模块运行python脚本。我几乎尝试了我在互联网上看到的所有例子,因此没有运气。在VBA模块中,我还运行.bat文件,它完美地运行:

batchname = "U:\Backup Bat File.bat"

Shell batchname, vbNormalFocus

Next I need to run the python script, which is located in the same folder as the excel file. Right now I'm trying out this:

接下来我需要运行python脚本,它与excel文件位于同一个文件夹中。现在我正在尝试这个:

Dim Ret_Val

Dim args

args=Activeworkbook.Path & "\beps_output.py"

Ret_Val = Shell("C:\python34\python.exe" & args, vbNormalFocus)

It doesn't error out, but nothing happens. I'm a little confused at what the "Ret_Val" (return value?) does here, and why it isn't running.

它没有错误,但没有任何反应。我对“Ret_Val”(返回值?)在这里做什么以及为什么它没有运行有点困惑。

2 个解决方案

#1


2  

This is running on my PC (Win 7 x64, python 2.7.9):

这是在我的电脑上运行(Win 7 x64,python 2.7.9):

Sub runpython()
    Dim Ret_Val
    Dim args As String

    args = "W:\programming\python\other_py\sqrt.py"
    Ret_Val = Shell("C:\Program Files (x86)\python27\python.exe" & " " & args, vbNormalFocus)
    If Ret_Val = 0 Then
       MsgBox "Couldn't run python script!", vbOKOnly
    End If
End Sub

Ret_Val will be non-zero if the call succeeds, namely the processID of the launched command. Note that the command will run asynchronuously i.e. the VBA code will continue faster than the external command will terminate.

如果调用成功,则Ret_Val将为非零,即已启动命令的processID。请注意,该命令将异步运行,即VBA代码将继续比外部命令终止更快。

#2


1  

Try adding a space between exe program and file:

尝试在exe程序和文件之间添加一个空格:

Ret_Val = Shell("C:\python34\python.exe " & args, vbNormalFocus)

Also, because Shell is a function that returns a value (variant type - double) with specified arguments, the returned value can be contained in a variable, here as you specify with Ret_Val. You can then add conditional logic and error handling using this value.

此外,因为Shell是一个返回带有指定参数的值(变量类型 - double)的函数,所以返回的值可以包含在变量中,就像使用Ret_Val指定的那样。然后,您可以使用此值添加条件逻辑和错误处理。

#1


2  

This is running on my PC (Win 7 x64, python 2.7.9):

这是在我的电脑上运行(Win 7 x64,python 2.7.9):

Sub runpython()
    Dim Ret_Val
    Dim args As String

    args = "W:\programming\python\other_py\sqrt.py"
    Ret_Val = Shell("C:\Program Files (x86)\python27\python.exe" & " " & args, vbNormalFocus)
    If Ret_Val = 0 Then
       MsgBox "Couldn't run python script!", vbOKOnly
    End If
End Sub

Ret_Val will be non-zero if the call succeeds, namely the processID of the launched command. Note that the command will run asynchronuously i.e. the VBA code will continue faster than the external command will terminate.

如果调用成功,则Ret_Val将为非零,即已启动命令的processID。请注意,该命令将异步运行,即VBA代码将继续比外部命令终止更快。

#2


1  

Try adding a space between exe program and file:

尝试在exe程序和文件之间添加一个空格:

Ret_Val = Shell("C:\python34\python.exe " & args, vbNormalFocus)

Also, because Shell is a function that returns a value (variant type - double) with specified arguments, the returned value can be contained in a variable, here as you specify with Ret_Val. You can then add conditional logic and error handling using this value.

此外,因为Shell是一个返回带有指定参数的值(变量类型 - double)的函数,所以返回的值可以包含在变量中,就像使用Ret_Val指定的那样。然后,您可以使用此值添加条件逻辑和错误处理。