我能够成功地实现这里提供的代码作为答案,每当一封名为“Blah”的电子邮件进入我的收件箱时,它都会运行一个 python 脚本。
现在,我正在尝试实现代码,该代码将在每次名为“Woo”的电子邮件进入我的收件箱中的子文件夹时在单独的Excel 电子表格上运行宏。main.xlsx
我已经设置productionItems = objectNS.GetDefaultFolder(olFolderInbox).Folders("Production Emails").Items
为抓取此子文件夹中的所有项目。作为实现这一目标的一个步骤,我希望每次名为“Woo”的邮件到达收件箱的“Production Emails”子文件夹时,至少打印一条 Debug.Print 消息(或消息框,随便什么)。但是,每次我向自己发送一封名为“Woo”的电子邮件时,我都没有收到预期的 Debug.Print “Arrived3”。有谁知道为什么我没有收到 Print 声明?
Option Explicit
Private WithEvents inboxItems As Outlook.Items
Private WithEvents productionItems As Outlook.Items [!!!]
Public Sub Application_Startup()
Dim outlookApp As Outlook.Application
Dim objectNS As Outlook.NameSpace
Set outlookApp = Outlook.Application
Set objectNS = outlookApp.GetNamespace("MAPI")
Set inboxItems = objectNS.GetDefaultFolder(olFolderInbox).Items
Set productionItems = objectNS.GetDefaultFolder(olFolderInbox).Folders("Production Emails").Items
End Sub
Private Sub inboxItems_ItemAdd(ByVal Item As Object)
On Error GoTo ErrorHandler
Dim Msg As Outlook.MailItem
Dim MessageInfo
Dim Result
If TypeName(Item) = "MailItem" Then
Debug.Print "Arrived3"
If Item.Subject = "Blah" Then
Const PyExe = "C:\...\python.exe"
Const PyScript = "R:\...\main.py"
Dim objShell As Object, cmd As String
Set objShell = CreateObject("Wscript.Shell")
cmd = PyExe & " " & PyScript
Debug.Print cmd
objShell.Run cmd
objShell.exec cmd
MsgBox objShell.exec(cmd).StdOut.ReadAll
End If
End If
ExitNewItem:
Exit Sub
ErrorHandler:
MsgBox Err.Number & " - " & Err.Description
Resume ExitNewItem
End Sub