How to run a macro on new mail arrival?

Y

yajiv.vijay

Is there any event like mail arrival. I want to run a macro on each
mail that arrives at my inbox, automatically. This is same as rules
but i dont know how to run a macro by rule.
 
S

Sue Mosher [MVP-Outlook]

A "run a script" rule action runs not an external script but a VBA procedure with a MailItem or MeetingItem as its parameter. That item is processed by the code, as in this example:


Sub RunAScriptRuleRoutine(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim msg As Outlook.MailItem
Dim rply as Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.Session
Set msg = olNS.GetItemFromID(strID)
' do stuff with msg, e.g.
Set rply = msg.Reply
rply.Body = "What you want the reply to say."
rply.To = "(e-mail address removed); (e-mail address removed)"
rply.Send

Set msg = Nothing
Set rply = Nothing
Set olNS = Nothing
End Sub

For Outlook VBA basics, see http://outlookcode.com/article.aspx?id=49ww.outlookcode.com/d/code/zaphtml.htm#ol2002 for another example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top