Outlook's Rules and Alerts: Run a Script

Last reviewed on February 13, 2013

Using the “Rules and Alerts” option to “run a script”, the list of scripts to run is empty. What makes a script appear in the list?

The argument must by type MailItem or MeetingItem for the subroutine to be available in the Rules Wizard in Outlook 2007. In Outlook 2010 and 2013, PostItem also works.

You need to avoid processing messages using more than one rule when you are using a run a script rule. Run a script rules should be placed at the top of the rules list and include the “stop processing” action.

All actions should be in the script – don’t combine actions in the rule with the run a script rule. Include all of the actions you want to take on the items that meet the rule’s conditions in the VBA procedure.

Something like this:

Public Sub ShowMessage(Item As Outlook.MailItem)

'code

End Sub

Using VBA Editor

Open Outlook’s VBA editor by pressing Alt+F11 and expand Microsoft Office Outlook Objects then double click on ThisOutlookSession.

Script in the VBA Editor

You’ll type or paste the code into the module, then create the rule with the ‘run script’ Action and select this script

Create a Run a Script Rule

After the script is written, you need to add it to a rule.

  1. Open Rules Wizard. In Outlook 2010 and 2013, it’s on Outlook’s Home ribbon,
    Rules > Manage Rules & Alerts. Look on the Tools menu in older versions.
  2. Open Rules Wizard

  3. Click New Rule.
  4. Select Apply Rule on messages I receive and click Next.
  5. Select your conditions and click Next.
  6. Select Run a script action (near the bottom).
  7. Click on a script.
  8. Select your script, click OK.
  9. The finished run a script rule

  10. Click Next then finish the rule.
  11. Select the script

For best results, all actions should be in the script.

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

22 responses to “Outlook's Rules and Alerts: Run a Script”

  1. Antonio

    What about runing scripts for OUTGOING messages? Any solution? Thanks

  2. Richard

    Using VBA to save an attachment with the rule wizard

  3. Andy

    Hi,
    I am currently trying to create a script that can be ran as part of rule within Outlook 2013. This will look for a certain subject within the message, then run a script to copy a folder from a server down to my local machine. I believe I have the correct code for this however the script does not appear for selection when creating the rule.

    The code reads -

    Dim fso As FileSystemObject
    Set fso = New FileSystemObject
    fso.MoveFolder "Source", "DeStination"
    Set fso = Nothing

    I have opened VB editor, opened ThisOutlookSession, pasted in the code (with the correct source and destination), then saved the VB project, but this still does not appear for selection. Any help would be greatly appreciated!

    Thanks,

  4. Mark W

    I got this to work the first time. It stopped working after I exited Outlook 2013 and went back in. Any ideas?

  5. Mark W

    That seems to have fixed it. Thanks!

  6. Al

    My rule doesn't trigger my VBA script when an email enters the Inbox. I know my script works because I can run the rule manually.

  7. Al

    Apply this rule after the message arrives
    from xxxxxx
    and with xxxxx in the subject
    and on this computer only run xxxxxxxxxxxxx

  8. Chris D

    Say I want to run my "script", which does subject line checking, on every message that comes in, but also allow additional rules to run on those messages that "fall through" my 'IF' statement in my code?

    For example, say I have a mail with the subject "Attachements included" that has attachments I want to save, and another that has the subject "Just Move Me" that I have a rule set for that moves it to a folder. I want the "script" to run on both, but if the subject is "Attachements included", save them (I already have working code for this) and move the message, otherwise, do nothing.

    I put my rule to "run a script" as follows:

    Apply this rule after the message arrives
    on this computer only
    run Project1.ThisOutlookSession.stripAttachmentsFromMailItem

    and another rule after it that says:

    Apply this rule after the message arrives
    with "Just Move Me" in the subject
    and on this computer only
    move it to the "Moved" folder

    My question is if I put "Stop processing more rules" in my "run a script" rule, the additional ones never fire, and I lose my move message functionality, unless I want to code in moves for all of my rules (which I really don't... lol).

    In small testing, it seems to work fine without it, but the warning above specifically states to not process additional rules on messages that have a "script" run on them. I guess I'm just wondering if my set up will work in the long run. Any advice? :)

  9. francesca

    I need to create more projects to manage different rules.
    How can i do?
    Please help me !

  10. Chris D

    Ahh, good to know. Thank you.

  11. Dallas M

    I am trying to change the save destination of the PDF. Where would I replace the folder information?

    P.S. I read and use your codes all the time! I have been able to create a few simple ones of my own because of you. Thanks!

  12. Egharevba Osarugue

    Hello,
    I am trying to find a way to accurate delete duplicate mails on outlook, or move them to a folder where i could review it before deleting it. Please could you help with a code that could do that or steps to follow to achieve that?

Leave a Reply

If the Post Coment button disappears, press your Tab key.