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

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?

Thanks,
Dave
LVL 2
Cristal638Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
David LeeConnect With a Mentor Commented:
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
    Next
    Set olkItem = Nothing
    Set olkItems = Nothing
    Set objShell = Nothing
End Sub
'Macro ends here
0
 
David LeeCommented:
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.

Cheers!
0
 
Cristal638Author Commented:
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

Thanks,
Dave
0
 
rockiroadsCommented:
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

eg.

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


0
All Courses

From novice to tech pro — start learning today.