在VBA Ou中运行Python或PowerShell

2024-06-01 02:14:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Outlook中使用VBA运行Python脚本。Python脚本使用restapi获取数据并写入系统中的文件。我用VBA来安排发送电子邮件。在

Python脚本(get_people.py)从操作系统运行。它调用restapi并获取人名列表并写入OS中的文件。在

我试过两种方法:

  1. 在名为getNames.bat的批处理文件中包装Python执行:

    #getNames.bat file
     C:\Python37\python.exe "C:\scripts\get_people.py"
    

    和VBA调用这个.bat文件:

^{2}$

但是,Python行不在VBA中运行,但是如果我从cmd.exe手动执行,它就会运行。在VBA中,命令屏幕迅速弹出并消失。在

  1. 直接在VBA中运行Python脚本:
    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"

但这也失败了。在


Tags: 文件py脚本getstringasvbapeople
2条回答

下面的怎么样?在

Option Explicit
Public Sub Example()
    Shell "cmd /k C:\Python37\python.exe C:\scripts\get_people.py", vbmaximizedfocus 
End Sub

最初的案例和我为我创建的代码非常相似。尝试在批处理文件的末尾加上“暂停”。在

trybat.bat公司

@echo off
echo "hello world"
pause

VBA代码

^{pr2}$

相关问题 更多 >