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

时间:2016-09-12 09:30:42

标签: vba outlook outlook-vba outlook-2010

我在Outlook 2010中设置了多个邮箱。当我在其中一个非默认邮箱上收到邮件时,我希望运行一个宏。

我已编码以下内容并将代码插入 "ThisOutlookSession"

我已将其用于默认邮箱的收件箱,但不是我的非默认邮箱的收件箱。当我尝试重新打开Outlook 2010插入代码时,它告诉我: "Compile error in hidden module: ThisOutlookSession".非默认框名为'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 :(得分: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