How do I create a macro to auto reply with a message based on on unique id string in subject?

I have less than 10 ids I'm looking for in subject lines and I need to respond with a unique message for each id. The trouble I'm having is that the auto-reply needs to go back to the "reply to" field not the sender's address. It's a dedicated box that runs 24/7 running XP Pro and Outlook 2003 checking a standard POP account. This box's sole function is to process rules. Any help with this and directions for the best book/resource so I won't have to bug EE again would be great.

Not sure if it matters but the ID is preceded by a sharp like "#123456". All ids are 6 digits.

I've made many attempts by piecing together code from all over EE and elsewhere. The IF statements are the thorn.

Who is Participating?
David LeeConnect With a Mentor Commented:
The code below should get the job done.  I say "should" because I haven't tested it.  Follow these instructions to add the code to Outlook.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects
4.  If not already expanded, expand Modules
5.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
7.  Edit the code as needed.  I included comments wherever something needs to or can change
8.  Click the diskette icon on the toolbar to save the changes
9.  Close the VB Editor

To use the code

1.  Create a rule that fires for all of these messages
2.  Set the rule's action to "run a script" and select this script as the one to run

Here's how this works.

1.  A message arrives and triggers the rule
2.  The rule runs the macro and passes the message to it as an argument
3.  The macro searches the subject line of the message for one of the unique ID's
4.  If an ID is found, then the macro creates and sends a reply using the message text defined for that ID

Keep in mind that this will trigger Outlook's built-in security causing the dialog-box I described to pop up.  
Sub AutomatedReply(Item As Outlook.MailItem)
    Dim olkReply As Outlook.MailItem, strMsg As String, olkRecipient As Outlook.Recipient
    'Create a block like this for each unique ID you need to search for'
    'Check for first unique ID.'
    'Edit the ID on the next line'
    If InStr(1, Item.Subject, "#123456") Then
        'The message may include HTML formatting tags if you plan to send it in HTML format'
        strMsg = "Some response for first ID"
    End If
    'Check for second unique ID'
    'Edit the ID on the next line'
    If InStr(1, Item.Subject, "#234567") Then
        strMsg = "Some response for second ID"
    End If
    'Check for third unique ID'
    'Edit the ID on the next line'
    If InStr(1, Item.Subject, "#345678") Then
        strMsg = "Some response for third ID"
    End If

    If strMsg <> "" Then
        Set olkReply = Application.CreateItem(olMailItem)
        With olkReply
            'Change the subject on the next line as desired'
            .Subject = "Your Subject Goes Here"
            For Each olkRecipient In Item.ReplyRecipients
                .Recipients.Add olkRecipient.Address
            'Change olFormatHTML to olFormatPlain if you want the message to go out in plain-text format'
            .BodyFormat = olFormatHTML
            'Change .HTMLBody to .Body if the message is plain-text'
            .HTMLBody = strMsg
        End With
    End If
    Set olkReply = Nothing
    Set olkRecipient = Nothing
End Sub

Open in new window

Have you tried create a rule in outlook for this purpose?
David LeeCommented:
Hi, charlyRoot.

Does each message come with a single ID in the subject line or are there multiple IDs?
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

commfirstAuthor Commented:
Always single ID.
Yes I've tried rules and responding with templates can't pull the addy from the reply-to field in the header...or can it? I could only get it to send back to
David LeeCommented:
I can do it with a bit of scripting.  The problem is that Outlook 2003 and earlier has a built-in security feature that prevents scripts from reading any field that may contain an address and from sending messages without the user's express consent.  In both cases Outlook displays a dialog-box warning that an application is attempting to access your mailbox and asking for your permission to allow it to continue.  That kills any automated process.  While there isn't any means of turning security off, there are ways around it.  Here are those ways.

1.  Sign the code.  Here's a link to instructions on doing that:
2.  Use ClickYes (, a small utility that'll click the Yes button for you.  It creates a security hole though, since a virus could start sending messages and ClickYes would click the Yes button for it too.  
3.  Use Redemption (, a COM library that enables code to safely bypass Outlook security.

If you want to proceed, then I can provide the script and instructions.
commfirstAuthor Commented:
The only infections I get in this building are from users OKing AVG look-alike dialogue boxes, thus allowing infection. Plus my ISP calls me with abnormal activity like mail spew (I left a Win2000 mail relay open a few years ago). I've seen these work arounds in other posts. I am going to sign the code. If that doesn't work for whatever reason, I was already prepared to use ClickYes.

The code would be greatly appreciated. I have about 2 months in this project. I was determined to piece it together from everything I could find here on EE and elsewhere, but nope. All I have done to this point is cause Outlook to crash.
commfirstAuthor Commented:
Thank you very very much. I'm starting testing tonight.
David LeeCommented:
You're welcome.
commfirstAuthor Commented:
Thanks bunches. I almost have it working and am confident I have all I need. You are a true asset to this EE.
David LeeCommented:
You're welcome.  Thank you for the generous comment.  That's worth more than the points.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.