【问题标题】:Run code when email shows up in Outlook subfolder当电子邮件出现在 Outlook 子文件夹中时运行代码
【发布时间】:2022-06-21 22:12:03
【问题描述】:

我实现了作为答案 here 提供的代码,以在每次主题为“Blah”的电子邮件进入我的收件箱时运行 Python 脚本。

我正在尝试实现代码,每当主题为“Woo”的电子邮件进入我的收件箱中的子文件夹时,就会在 单独的 Excel 电子表格 main.xlsx 上运行宏。 p>

要获取此子文件夹中的所有项目,我有

Set productionItems = objectNS.GetDefaultFolder(olFolderInbox).Folders("Production Emails").Items

作为实现目标的一步,我想在每次名为“Woo”的邮件到达收件箱的“Production Emails”子文件夹时生成一条带有Debug.Print(或消息框)的消息。

当我向自己发送一封主题为“Woo”的电子邮件时,我没有收到 Debug.Print 消息“Arrived3”,这是我所期望的。

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

【问题讨论】:

  • Private Sub inboxItems_ItemAdd(ByVal Item As Object) 替换为Private Sub productionItems_ItemAdd(ByVal Item As Object)

标签: excel vba outlook


【解决方案1】:

首先,在代码中,您为收件箱文件夹而不是子文件夹设置了ItemAdd 事件处理程序。如果要从子文件夹接收事件,则需要更改事件处理程序的名称。

如果您从 Excel 自动化,您需要在代码中创建一个新的 Outlook Application 实例:

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 = New 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 productionItems_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

您的 VBA 宏似乎设计为从 Outlook 而非 Excel 运行。不要忘记您需要从 Excel 中调用 Application_Startup 方法。

【讨论】:

  • 非常感谢!这个解决方案对我有用 - 现在我在收件箱和子文件夹中收到的电子邮件上运行了 excel 宏。
【解决方案2】:

Outlook 代码如下所示。

Option Explicit

Private WithEvents productionItems As Items

Private Sub Application_Startup()
    Dim myInbox As Folder
    Set myInbox = Session.GetDefaultFolder(olFolderInbox)
    Set productionItems = myInbox.Folders("Production Emails").Items
End Sub


Private Sub productionItems_ItemAdd(ByVal Item As Object)

Dim Msg As MailItem

'On Error GoTo ErrorHandler     ' comment while in development

If TypeOf Item Is MailItem Then

    Debug.Print "Arrived3"
    
    Set Msg = Item
    If Msg.Subject = "Blah" Then
        With Msg
            Debug.Print " Subject.....: " & .Subject
            Debug.Print " ReceivedTime: " & .ReceivedTime
            ' code to run main.xlsx
        End With
    End If
End If

ExitNewItem:
    Exit Sub
    
ErrorHandler:
    MsgBox err.Number & " - " & err.Description
    Resume ExitNewItem
    
End Sub


Private Sub test()
    productionItems_ItemAdd ActiveInspector.CurrentItem
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-07
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多