收到指定主题或发件人的电子邮件后显示MsgBox

时间:2014-08-26 10:37:27

标签: vba outlook mailitem

如何在收到指定主题或发件人的邮件时显示MsgBox或提醒?

我把这个程序放在ThisOutlookSession块中。

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
    Dim myMail As MailItem
    Dim name As String

    If TypeOf Item Is MailItem Then
        Set myMail = Item
        If myMail.Subject Like "*Hello world*" And myMail.Categories = "" Then
            MsgBox "Message", vbInformation, "approved"
            MailDate = myMail.ReceivedTime
            myMail.Categories = "CZEART"
            myMail.MarkAsTask (olMarkNoDate)
            myMail.Save
        End If
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

要测试代码,请打开包含所需条件的mailitem,然后逐步执行此操作。

Option Explicit

Private Sub test()
    Dim currItem As MailItem
    Set currItem = ActiveInspector.currentItem
    olInboxItems_ItemAdd currItem
End Sub

可能在ThisOutlookSession模块中需要这个。

Option Explicit

Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
  Dim objNS As NameSpace
  Set objNS = Application.Session
  ' instantiate objects declared WithEvents
  Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
  Set objNS = Nothing
End Sub

http://www.outlookcode.com/article.aspx?id=62