当邮件到达非默认邮箱时,Outlook运行宏 [英] Outlook run macro when mail arrives on a nondefault mailbox

查看:109
本文介绍了当邮件到达非默认邮箱时,Outlook运行宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have multiple mailboxes set-up in my Outlook 2010. I would like a macro to run when I receive a mail on one of the non-default mailboxes.

我已经对以下代码进行了编码,并将代码插入了 "ThisOutlookSession" 中.

I have coded the below and inserted the code into "ThisOutlookSession".

我已将其用于默认邮箱的收件箱,但不适用于我的非默认邮箱的收件箱.当我尝试重新打开已插入代码的Outlook 2010时,它告诉我: "Compile error in hidden module: ThisOutlookSession".非默认框称为"abc.asia".

I have gotten it to work for the default mailbox's inbox but not my nondefault mailbox's inbox. When I try to re-open outlook 2010 having inserted the code, It tells me : "Compile error in hidden module: ThisOutlookSession". The non-default box is called 'abc.asia'.

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

I am new to vba so any inputs are appreciated, thank you!

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

推荐答案

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

Update Set olRecip = olNs.CreateRecipient("emal@address.com") with correct Email address.

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

这篇关于当邮件到达非默认邮箱时,Outlook运行宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆