Run a program after email arrival

Dear Experts,

I am looking for a script/macro which would process the contents of incoming emails and execute a certain application when a certain condition is met.

Your help will be greatly appreciated!

Thanks
Talal216Asked:
Who is Participating?
 
David LeeCommented:
Sorry to be slow.  I think this will do it.  I say "think" because I haven't tested it.  Follow these instructions to use it.

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects
4.  If not already expanded, expand Modules
5.  Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
6.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
7.  Edit the code as needed.  I included comments wherever something needs to or can change
8.  Click the diskette icon on the toolbar to save the changes
9.  Close the VB Editor
10. Create a rule that fires for all messages
11. Set the rule's action to "run a script" and select this script as the one to run
Sub Talal216(Item As Outlook.MailItem)
    Dim olkItem As Outlook.MailItem, _
        olkReply As Outlook.MailItem, _
        objFSO As Object, _
        objFile As Object, _
        objShell As Object
    If InStr(1, Item.subject, "DECEMBER 2009 REPORT for Shipping Department") Then
        Set olkItem = Session.GetDefaultFolder(olFolderInbox).Items.Find("[SenderEmailAddress]='" & Item.SenderEmailAddress & "' AND [ReceivedTime] >= '" & Format(DateAdd("h", 1, Item.ReceivedTime), "ddddd h:nn AMPM") & "'")
        If TypeName(olkItem) = Nothing Then
            Set olkReply = Application.CreateItem(olMailItem)
            With olkReply
                .Recipients.Add Item.SenderEmailAddress
                .Recipients.ResolveAll
                'Edit the subject on the next line as desired'
                .subject = "DECEMBER 2009 REPORT for Shipping Department"
                'Edit the message body on the next line as desired'
                .Body = "The report will be ready in 1 hours time."
                .Send
            End With
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            'Edit the file name and path on the next line as needed'
            Set objFile = objFSO.CreateTextFile("C:\myscripts\shipping\somefile.txt", True)
            objFile.WriteLine Month(Date) & Year(Date)
            objFile.Close
            Set objShell = CreateObject("WScript.Shell")
            'Edit the program name and path on the next line as needed'
            objShell.Run "c:\myscripts\shipping\someprogram.exe", 2, False
        Else
            Set olkReply = Application.CreateItem(olMailItem)
            With olkReply
                .Recipients.Add Item.SenderEmailAddress
                .Recipients.ResolveAll
                'Edit the subject on the next line as desired'
                .subject = "DECEMBER 2009 REPORT for Shipping Department"
                'Edit the message body on the next line as desired'
                .Body = "Cannot process request,earlier report still processing."
                .Send
            End With
        End If
    End If
    Set olkItem = Nothing
    Set olkReply = Nothing
    Set objFSO = Nothing
    Set objFile = Nothing
    Set objShell = Nothing
End Sub

Open in new window

0
 
David LeeCommented:
Hi, Talal216.

I might be able to help with that.  I'd need to know more about the condition and details of the program you want to run.
0
 
Talal216Author Commented:
Hey BlueDevilFan,

Its great to hear from you again :)

Okie.I hope you can help me on this as i am pretty much packed with deadlines and i need a good experts help.

The steps to be followed are :-
1)User sends email to Support@xyz.com
2)The processor/script is running for each email received.
3)When the processor finds the following words "DECEMBER 2009 REPORT for Shipping Department"
4)An email is sent back to the user saying that the report will be ready in 1 hours time.
5)If the same request is received in less than one hours time,the script will send the message back saying "Cannot process request,earlier report still processing."
6) A text file is opened in the directory (c:\myscripts\shipping) and the MONTH and Year is put in that.
7)The exe file located in the c:\myscripts\shipping folder is started which will generate the report which can be send later.

Note:the .exe file is not an excel file.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
David LeeCommented:
Everything but #5 is pretty simple.  What happens to a message after it's been received and responded to?  Also, please refresh my memory on the version of Outlook you're using.
0
 
Talal216Author Commented:
The exe file  needs about 45 minutes to run.if the exe files are executed twice it might overload the application and it might crash. The exe file , basically after execution will invoke an excel macro which will do further calculations on the report and the compiled report would be sent as attachment. back to the user.

A small idea for #5 would be when the text file is made, the time of the execution  is also put along with the Month and Year.So when the script is executing again it will check the time and compare the difference.

I am using Outlook 2007.

Thanks!

0
 
Talal216Author Commented:
The message is closed after it is responded in step #4 or #5 depending on the situation.
0
 
Talal216Author Commented:
Hi BlueDevilFan,

Do you think  my idea /suggestion is fine or do you think there is a better way to solve that?

Thanks!

0
 
Talal216Author Commented:
Hey not a problem BDF :)


Thank you very much for the script but i hit an compile error indicating invalid use of an object at line number 9 in the above script.Please assit me to sort it out :)

Thanks once again BlueDevilFan.
0
 
David LeeCommented:
The word Nothing should be in quotes.  Like this

If TypeName(olkItem) = "Nothing" Then
0
 
Talal216Author Commented:
Dear BlueDevilFan,

First of all Seasons greetings to you!

It works like a charm.

Perfecto !.

You are the real Guru,I would be extending this script to suit my requirements,if i get stuck i will get back to you and probably pop up another question.

Thanks ones again.I am awarding you the full points.

0
 
Talal216Author Commented:
Expert is truly a guru
0
 
David LeeCommented:
Thanks, Talal216.  Glad I could be of assistance.  Seasons greetings to you too!
0
 
Talal216Author Commented:
Dear Blue Devil Fan,

Could you please explain what exactly happens in line 8 and the following subsequent line 9 ?Others i can easily follow thanks to your valuble comments.


0
 
David LeeCommented:
Line 8 searches for an item from the same sender received within the last hour.  It addresses your requirement #5.  Line 9 tests to see if one was found.
0
 
Talal216Author Commented:
Thanks  for clarifying ,much appreciated.

0
 
David LeeCommented:
You're welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.