Solved

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

Posted on 2006-06-21
4
241 Views
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?

Thanks,
Dave
0
Comment
Question by:Cristal638
[X]
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
4 Comments
 
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.

Cheers!
0
 
LVL 2

Author Comment

by:Cristal638
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

Thanks,
Dave
0
 
LVL 65

Expert Comment

by:rockiroads
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

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
 
LVL 76

Accepted Solution

by:
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
    Next
    Set olkItem = Nothing
    Set olkItems = Nothing
    Set objShell = Nothing
End Sub
'Macro ends here
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
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…
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 …

728 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