Running a macro if new mail if from a specific address

Hello,

I'm currently trying to write a macro that is run when new mail is received in Outlook 2002 (possibly XP not sure though)

I would like the macro to check if the new email is from a specific address (in this case sales@falcontrunking.co.uk), delete the email, and display a message saying "There is a new message in the Sales Inbox".

I know how to run a macro when a new message is received and I know how to show a message box saying "There is a new message in the Sales Inbox" but I don't know how to check if the new message has been received from a specific address.

Can anyone help me



Answer this question

Running a macro if new mail if from a specific address

  • Deac910

    Sorry. I'm running Outlook 2003 here, and I shouldn't have assumed that the NewMailEx event exists in older versions. The problem with the NewMail event is that it doesn't provide you with information about which messages have just arrived. NewMailEx does that. I'll look into this some more and get back to you.



  • GreatDane

    Sorry, I didn't make it clear how little I know.

    Could you explain how to do this


  • Jan Kučera

    I've tried your code and it doesn't even run when a new email is received.

    I've noticed that you have put Application_NewMailEx(ByVal EntryIDCollection As String) when the sub that would run on recepit of a new email would be Application_NewMail().

    This is the first time I've ever tried to make a macro for Outlook and my knowledge of VBA is strictly limited to Excel.

    If I changed the Application_NewMailEx(ByVal EntryIDCollection As String) bit to Application_NewMail() when a new email is received it brings up an error message saying "Compile error: Variable not defined" and highlights nCommaLoc = InStr(nPtr, EntryIDCollection, ",")

    Have you any suggestions how to rectify this



  • NetPochi

    Create a MailItem object from the new message, and compare the MailItem.SenderEmailAddress property to the string you're looking for.



  • stubs

    Thank you very much!
  • Johan_SQLDBA

    This should do what you're looking for. You may need to adapt it slightly to fit your project, but it should give you a good starting point.

    Option Explicit

    Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
    Dim mi As MailItem, nCommaLoc As Integer, nPtr As Integer, EntryID As Variant, _
    EntryIDs As Collection

    Set EntryIDs = New Collection
    nPtr = 1
    Do
    nCommaLoc = InStr(nPtr, EntryIDCollection, ",")
    If nCommaLoc = 0 Then
    EntryIDs.Add Mid(EntryIDCollection, nPtr, Len(EntryIDCollection) - nPtr + 1)
    Exit Do
    End If
    EntryIDs.Add Mid(EntryIDCollection, nPtr, nCommaLoc - 1)
    nPtr = nCommaLoc + 1
    Loop
    For Each EntryID In EntryIDs
    Set mi = Application.Session.GetItemFromID(EntryID)
    If InStr(1, mi.SenderEmailAddress, "TEST") Then
    ' your code here
    End If
    Next EntryID
    End Sub



  • Running a macro if new mail if from a specific address