(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
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 500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

856 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