首页 文章

当邮件到达非默认邮箱时,Outlook 运行宏

提问于
浏览
1

我的 Outlook 2010 中有多个邮箱 set-up。当我在 non-default 邮箱之一上收到邮件时,我希望运行一个宏。

我已经对下面的代码进行了编码,并将代码插入到**“ ThisOutlookSession”**中。

我已经将其用于默认邮箱的收件箱,但不适用于我的非默认邮箱的收件箱。当我尝试 re-open Outlook 2010 已插入代码时,它告诉我:"Compile error in hidden module: ThisOutlookSession". non-default 框称为“ abc.asia”。

我是 vba 的新手,因此感谢您的投入,谢谢!

Dim WithEvents myInboxMailItem As Outlook Items

Private Sub myInboxMailItem_ItemAdd(ByVal Item As Object)
    MsgBox("Item Added")
End Sub

Private Sub Initialize_Handler()
    Dim fldInbox As Outlook.MapiFolder
    Dim gnspNameSpace As Outlook.NameSpace

    Set gnspNameSpace = Outlook.GetNameSpace("Mapi")
    Set fldInbox = gnspNameSpace.Folders("abc.asia").Folders("Inbox")
    Set myInboxMailtItem = fldInbox.Items

End Sub

1 回答

  • 0

    使用正确的电子邮件地址更新Set olRecip = olNs.CreateRecipient("emal@address.com")

    Option Explicit
    Private WithEvents Items As Outlook.Items
    
    Private Sub Application_Startup()
        Dim olNs As Outlook.NameSpace
        Dim Inbox  As Outlook.MAPIFolder
        Dim olRecip As Recipient
    
        Set olNs = Application.GetNamespace("MAPI")
        Set olRecip = olNs.CreateRecipient("emal@address.com")  '// Owner's Name or email address
        Set Inbox = olNs.GetSharedDefaultFolder(olRecip, olFolderInbox)
        Set Items = Inbox.Items
    End Sub
    
    Private Sub Items_ItemAdd(ByVal Item As Object)
        If TypeOf Item Is Outlook.MailItem Then
            Debug.Print Item.Subject
        End If
    End Sub
    

相关问题