Running a Macro when email arrives

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to run a macro when an email with a specific Subject arrives, how can
I check every email that comes in?

Thanks!!
 
A "run a script" rule action takes a MailItem or MeetingItem as its parameter, then uses that item in the code:

Sub RunAScriptRuleRoutine(MyMail As MailItem)
Dim strID As String
Dim olNS As Outlook.NameSpace
Dim olMail As Outlook.MailItem

strID = MyMail.EntryID
Set olNS = Application.GetNamespace("MAPI")
Set olMail = olNS.GetItemFromID(strID)
' do stuff with olMail, e.g.
MsgBox olMail.Subject

Set olMail = Nothing
Set olNS = Nothing
End Sub

See http://www.outlookcode.com/d/code/zaphtml.htm#ol2002 for another example and alternative approaches.
 
Set whatever conditions you like in the rules wizard, no doubt a condition to check the subject for particular text, given what you described originally as your scenario.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Thanks!! This was really helpful

Sue Mosher said:
Set whatever conditions you like in the rules wizard, no doubt a condition to check the subject for particular text, given what you described originally as your scenario.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Sue,

Can I step back a bit on to a more basic level?

I've got an Outlook macro which I've successfully operated via a toolbar
button to check that it works. Like mecg96, I want it to run when a rule is
processed. I've created the rule up to the point of specifying the action to
take.

However, when I select "run a script" here, I get a window entitled "Select
Script" with an empty box underneath; there's nothing to click on in the box
and the only options I have are two buttons: OK and Close.

How do I link my macro to the rule? Should I be putting my macro script
somewhere special rather than in the "ThisOutlookSession" procedure in VBA?
 
Your "macro" needs to be in the format I described earlier -- a Public Sub with a single MailItem argument. In other words, the procedure structure is not the same as that for a macro that you can run from a toolbar button (which is a Public but argumentless Sub).
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Sue,

Apologies for not responding sooner - I didn't get a notification (maybe our
spam filters are overzelous) and haven't had a chance to check the site until
today.

Thanks very much for your suggestion which, although a little daunting for a
self-taught novice, looks feasible. I'll give it a try, and if you hear
nothing more from me you may assume
a) that it worked, and
b) that I'm grateful!

Giles.
 
Back
Top