我试图在Outlook中使用VBA运行Python脚本。Python脚本使用restapi获取数据并写入系统中的文件。我用VBA来安排发送电子邮件。在
Python脚本(get_people.py
)从操作系统运行。它调用restapi并获取人名列表并写入OS中的文件。在
我试过两种方法:
在名为getNames.bat
的批处理文件中包装Python执行:
#getNames.bat file
C:\Python37\python.exe "C:\scripts\get_people.py"
和VBA调用这个.bat文件:
但是,Python行不在VBA中运行,但是如果我从cmd.exe
手动执行,它就会运行。在VBA中,命令屏幕迅速弹出并消失。在
Sub GetNames()
Dim wsh as Object
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Dim sScript As String
Dim PyExe As String
Dim PyScript As String
Set wsh = VBA.CreateObject("WScript.Shell")
Set PyExe = "C:\Python37\python.exe"
Set PyScript = "C:\scripts\get_people.py"
wsh.Run PyExe & " " & PyScript
....
'This one crashes Outlook
...
Exit Sub
我在.bat文件中尝试了PowerShell命令而不是Python:
#getNames.bat file
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "Get-Date"
但这也失败了。在
下面的怎么样?在
最初的案例和我为我创建的代码非常相似。尝试在批处理文件的末尾加上“暂停”。在
trybat.bat公司
VBA代码
^{pr2}$相关问题 更多 >
编程相关推荐