(500 pts.) Use Outlook 2003 to trigger an event

Posted on 2006-06-21
Last Modified: 2010-08-05
Hello all,
This might be an easy 500 pts. for you.  I just need it right away.  FYI - MS Outlook 2003 on Windows XP Pro.  Anyway, here's what I'm looking to do:
Everyday I receive an automatic email notification letting me know that my files are ready.  Each email has the same exact sender, subject and verbiage.  The only difference is the time it's received.  I want to be able to automatically run a batch file as soon that email is received.  Is this possible and if so, how?

Question by:Cristal638
  • 2
LVL 76

Expert Comment

by:David Lee
Comment Utility
Hi Cristal638,

It is possible via a bit of scripting.  The script watches for a message with specific characteristics, e.g. the ones you described above, and then use a shell object to launch the batch file.  For this to work Outlook has to be open and running.  It will not work otherwise.  I can provide the script if you're interested.


Author Comment

Comment Utility
Can this be done in VBA code.  If so could you send a sample that looks for an email with the subject line of "This is a test" and then runs a batch file called c:\test.bat

LVL 65

Expert Comment

Comment Utility
In the VBA window, in Module ThisOutlookSession

there is the event handler  Application_NewMail

this is called whenever u get new mail

so u can check this and do your action

but first you must initialise some variables


Dim olNS As NameSpace, olF As MAPIFolder, olMail As Object

Private Sub Application_Startup()
     Set olNS = Application.GetNamespace("MAPI")
     Set olF = olNS.GetDefaultFolder(olFolderInbox)
End Sub

Private Sub Application_NewMail()
     Set olMail = olF.Items(1)
     MsgBox "Mail Received from " & olMail.SenderName
End Sub

now u can check olMail and do whatever u want

LVL 76

Accepted Solution

David Lee earned 500 total points
Comment Utility
Here's how I'd do it.  Follow these instructions to se this up.

1.  Start Outlook
2.  Click Tools->Macro->Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
4.  Copy the code below and paste it into the right-hand pane of the VB Editor window
5.  Edit the code making the changes per the comments I embeded in the code
6.  Click the diskette icon on the toolbar to save the changes
7.  Close the VB Editor
8.  Click Tools->Macro->Security
9.  Set the Security Level to Medium
10.  Close Outlook
11.  Start Outlook
12.  Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run.  Say yes.
13.  Test the macro by having the sender you're keying on send you a message with the specified subject.
14.  When the message arrives you should see the application you specified run.  

The macro is simple.  Here's how it works.
1.  When an item arrives in the Inbox the macro is triggered.
2.  The macro checks to see if there are any unread messages in the inbox from the designated sender, and with designated subject.
3.  If there are, then it runs the specified application.
4.  If there were no messages from the designated sender, then nothing occurs.

'Macro begins here
Private WithEvents olkInbox As Items

Private Sub Application_Startup()
    Set olkInbox = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Application_Quit()
    Set olkInbox = Nothing
End Sub

Private Sub olkInbox_ItemAdd(ByVal Item As Object)
    Dim olkItems As Outlook.Items, _
        olkItem As Object, _
        objShell As Object, _
        strCondition As String, _
        strCommand As String, _
        varRetVal As Variant
    Set objShell = CreateObject("Wscript.Shell")
    'Change the sender's name on the next line to that of the sender you want to key on
    strCondition = "[SenderName] = ""Bugs Bunny"" AND [Unread] = True AND [Subject] = ""I Love Carrots"""
    'Change the file name and path on the next line to that of the file you want to run
    strCommand = "C:\eeTesting\Listfiles.cmd"
    Set olkItems = olkInbox.Restrict(strCondition)
    For Each olkItem In olkItems
        If olkItem.Class = olMail Then
            varRetVal = objShell.Run(strCommand, , True)
            If varRetVal <> 0 Then
                MsgBox "Error running command"
            End If
        End If
    Set olkItem = Nothing
    Set olkItems = Nothing
    Set objShell = Nothing
End Sub
'Macro ends here

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now