« VB add Error Handling to a class | Main | Agile Manifesto »

October 18, 2002

Outlook New Mail Responder

This module responds to new mail events in Outlook, allowing you to create unique rules. I experimented with some coloring using one color for emails sent directly from my boss.

This code shows some samples of what you might do with that. I am setting a user property, called "ToWho" and then I use that property in a view in Outlook to color different mail items. But, theoretically, you could extend this to send a fax to your accountant if you get mail from the IRS. If you do something cool with, let me know. I would love to extend this to do really neat things.

' This module responds to new mail events, allowing you to 
' create unique rules.  I experimented with some coloring
' using one color for emails sent directly from my boss.
'
'
Public WithEvents myOlItems As Outlook.Items

Public Sub Initialize_handler()
    Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Application_NewMail()
  'MsgBox "New mail!"
  Beep
End Sub

Private Sub Application_Startup()

   ' Reference the items in the Inbox. Because myOlItems is declared
   ' "WithEvents" the ItemAdd event will fire below.
   Initialize_handler
End Sub

Private Sub myOlItems_ItemAdd(ByVal Item As Object)
  ' Check to make sure it is an Outlook mail message, otherwise
  ' subsequent code will probably fail depending on what type
  ' of item it is.
  
  If TypeName(Item) = "MailItem" And 1 = 2 Then
    Set mi = Item
    
    ' Set UserProperties so that mail can be color coded.
    mi.UserProperties.Add "ToWho", olText
    ' Set mail directly to you
    If (InStr(1, mi.To, "ctulino", vbTextCompare) Or _
      InStr(1, mi.To, "tulino", vbTextCompare) Or _
      InStr(1, mi.To, "ChrisAndDori", vbTextCompare)) Then
      mi.UserProperties.Item("ToWho") = "TO"
    ' Set mail copied to you
    ElseIf (InStr(1, mi.CC, "ctulino", vbTextCompare) Or _
      InStr(1, mi.CC, "tulino", vbTextCompare) Or _
      InStr(1, mi.CC, "ChrisAndDori", vbTextCompare)) Then
      mi.UserProperties.Item("ToWho") = "CC"
    Else
    ' otherwise
      mi.UserProperties.Item("ToWho") = "NOT"
    End If

    ' Create a message box if my boss sends mail directly to me.
    If InStr(1, mi.SenderName, "bossesname", vbTextCompare) And _
      (mi.UserProperties.Item("ToWho") = "TO") Then
      MsgBox "Moser sent mail.", vbOKOnly, "Boss Alert"
    End If

    'Choose message color

    mi.Save

  End If
End Sub

Posted by Chris at October 18, 2002 03:01 PM

Subscribe to this entry:   Email address:   

Trackback Pings

TrackBack URL for this entry:
http://www.christulino.com/cgi-bin/mt/mt-tb.cgi/28

Comments

this better be
good

Thats all I can say

Posted by: eccie at June 23, 2003 09:06 AM

Hey,
Huv u doing?I went thru the article "Outlook Mail Responder", pretty interesting and I also tried 2 use the code but it doesn't seem to work 4 me.It doesn't mean that ur code is wrong, it wld have been better if mentioned how to use the code and where 2 use it.Otherwise, this article is very informative and I also appreciate u 4 bringing ur code public which probable can become a good repository of valuable information for novice programmers.If u can mail me bak on how to use the code(Outlook Mail Responder)that'll b greatly appreciated.

Posted by: Siva at September 5, 2003 05:11 AM