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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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.
David LeeCommented:
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Email Protocols

From novice to tech pro — start learning today.