【发布时间】:2021-09-26 06:50:38
【问题描述】:
我正在尝试在将电子邮件移动到某个文件夹时触发 Python 脚本。
这个想法是在 Outlook 中设置一个规则,当电子邮件包含某些关键字时将其移动到特定文件夹,然后在将新项目添加到文件夹时让 VBA 脚本启动带有参数的 Python 脚本。我关注this guide.
Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items
Private Sub Application_Startup()
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")
Set objWatchFolder = objNS.GetDefaultFolder(olFolderInbox)
Set objItems = objWatchFolder.Items
Set objWatchFolder = Nothing
End Sub
Private Sub objItems_ItemAdd(ByVal Item As Object)
MsgBox "Message subject: " & Item.Subject & vbcrlf & "Message sender: " & Item.SenderName &" (" & Item.SenderEmailAddress & ")"
Dim subject As Item.subject
Shell("""C:\Path\to\python.exe"" ""C:\Path\With Spaces\script.py """ & subject)
Set Item = Nothing
End Sub
以前,在使用 Dim subject As Item.subject 声明主题变量后,以下工作有效:
Shell("""C:\path\to\python.exe"" ""C:\Path\With Spaces\To\Script\Hello.py """ & subject)
现在我得到一个编译错误
用户定义类型未定义。
此错误发生在Dim subject As Item.subject 行。
当我尝试通过以下方式将电子邮件的主题传递给 Python 脚本时:
Shell("""C:\path\to\python.exe"" ""C:\Path\With Spaces\To\Script\Hello.py """ & Item.subject)
它没有崩溃,但看起来 Python 脚本从未运行过。
当我这样做时:
Shell("""C:\path\to\python.exe"" ""C:\Path\With Spaces\To\Script\Hello.py "" & Item.subject")
它将 & 和 Item.subject 作为字符串参数传递 - 存储在 Item.subject 中的电子邮件主题没有传递,而是文字字符串“Item.subject”。
为什么Dim subject As Item.subject 现在会导致错误?如何将电子邮件的主题作为参数传递给我的 Python 脚本?
【问题讨论】:
-
代码图片没用。请看这里:stackoverflow.com/help/how-to-ask
-
@Tragamor:谢谢,我编辑了问题以摆脱屏幕截图。