Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2006-06-21
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 76

Expert Comment

by:David Lee
ID: 16954265
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

ID: 16954457
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

ID: 16954679
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 1500 total points
ID: 16955684
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you have clients or friends from around the world, it becomes a challenge to arrange a meeting or effectively manage your time. This is where Outlook's capability to show 2 time zones in one calendar comes in handy.
With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

596 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