Macro to sttart each time, a new mail arrives in a certain folder

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

Guest

Hello,

I'm looking for a reliable way which automatically runs a given macro every
time, a new eMail arrives in a given folder.

From all I know there is the newMail event - but as far as I know it is not
fully reliable.

Does anyone know of any reliable alternative?

Any help is greatly appreciated :-)

Best regards,
Tobias.
 
Outlook version? Are you putting the incoming message in the folder with a rule?

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Hi Sue,

thanks a lot for your quick reply.

Outlook 2003, eMails are put into folder by rule (if phrase xyz is in
subject, then move eMail in folder abc...)
 
In that scenario, you could also add a "run a script" action to the rule. 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.

You may want to include the code to move the item in the VBA procedure rather than including it as a separate action on that rule. I'm not sure how well mixing rule actions and a VBA procedure will work.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Hi Sue,

thanks a lot again.

It sound like a good idea to "simply" add the "run a script" action to my
existing rule. If I understood you correctly, I would need to enhance my rule
by pointing to the respective script. Is that right?

Does that script have to be located anywhere in particular? Could I just
include it in "ThisOutlookSession"?

My original problem was that I had defined a rule (which apparently always
worked if a new mail arrived in my postbox) and that I had written a script -
which sometimes didn't start. So I was looking for a solution to this
problem.

From all I can tell, your "first" method could be a solution to this -
provided mixing code and a rule does work...

However, if I wanted to implement your second suggestion, I would again have
to write a script only. And wouldn't that put me once again in the same
position that I am in now - which means I'd run the risk of the script not
always starting automatically?

Thank you very much for any help.

Best regards,
Tobias.
 
The VBA procedure that you want the rule to run can be in either ThisOutlookSession or a normal module that you've added. The Rules Wizard will display an error if you create a "run a script" rule and try to save it without pointing to the VBA procedure you want to run.

I was not suggesting mixing code and a rule. I was suggesting that you not mix normal rule actions and a "run a script" action. Instead, have the "script" perform all the actions that you want to take on the message. As long as the rule fires, the "script" will run.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Hi again,

I think you misunderstood me. I took the "run a script" option as option one
(which I though you did NOT prefer, since it would mean mixing a script & a
rule).

A second option (which I thought was the one you preferred) was to not use
any rule but write a macro that does the job of both (i.e. the rule and the
script).

However, my original problem was / is that at present I have problems with a
macro that does NOT automatically start every time a new mail enters. And I
fear that if I used option two, this problem could persist?!
 
A second option (which I thought was the one you preferred) was to not use
any rule but write a macro that does the job of both (i.e. the rule and the
script).

Yes, sort of. The code would be part of the rule, through the "run a script" action.
However, my original problem was / is that at present I have problems with a
macro that does NOT automatically start every time a new mail enters. And I
fear that if I used option two, this problem could persist?!

That's why I suggested that you switch to a "run a script" rule and let the rule handle running the script.
 
Thanks - seems I misunderstood you.

I quite like the run-a-script idea and I'll test it :-)

As the rule hasn't failed so far, I think (and hope...) it's the solution I
was after :-)
 
Back
Top