Search code examples
vbaoutlookoutlook-2010

How to create a rule for auto reply from a specific sender/subject


I receive an email alert from NMS every day when a link/connection to a branch is down. The subject email for all alerts are same, only the branch code is changed.

For example :

branch code : A01 and A02.

Subject email is " Connection to Branch A01 is Down " or "Connection to Branch A02 is Down".

For first handling, I must reply to it and ask the local contact to check devices.

I want to create a rule for auto reply. The body email is the same, but contains different branch code and PIC.

Template for body email is :

Dear "PIC",

based on email alert, Connection to the branch "A02" is down. Please restart the modem, etc.

I want to change PIC and code branch.


Solution

  • Create Script rule and call the following vba.

    Option Explicit
    Public Sub Rply(olItem As Outlook.MailItem)
        Dim olReply As MailItem
    
        '// Branch A01
        If InStr(olItem.Subject, "Connection to Branch A01 is Down") Then
            Set olReply = olItem.ReplyAll
            olReply.Display
            olReply.Body = "Dear PIC " & vbNewLine & vbNewLine & _
                           "Based on email alert, Connection to the Branch A01 is down " & vbNewLine & vbNewLine & _
                           "Please restart the modem, etc. " & vbNewLine & vbNewLine
            '// Send
            olReply.Send
        Else
            '// Branch A02
            If InStr(olItem.Subject, "Connection to Branch A02 is Down") Then
                Set olReply = olItem.ReplyAll
                olReply.Display
                olReply.Body = "Dear PIC " & vbNewLine & vbNewLine & _
                               "Based on email alert, Connection to the Branch A02 is down " & vbNewLine & vbNewLine & _
                               "Please restart the modem, etc. " & vbNewLine & vbNewLine
                '// Send
                olReply.Send
            End If
        End If
    End Sub