Ova

How do I add run a script in Outlook Rules?

Published in Outlook Automation 5 mins read

To add and run a script in Outlook Rules, you first need to enable the "run a script" option in the registry, then create your Visual Basic for Applications (VBA) script, and finally configure an Outlook rule to execute it.

How to Add and Run a Script in Outlook Rules

Running a script directly from Outlook rules can automate various email management tasks, from custom notifications to complex data processing. This functionality is often disabled by default for security reasons and requires a specific registry modification to enable.

1. Enable "Run a Script" Option via Registry Editor

Outlook's "run a script" action for rules is typically disabled by default in newer versions due to security concerns. You must enable it through the Windows Registry.

  1. Open Registry Editor:
    • Press Windows Key + R to open the Run dialog.
    • Type regedit and press Enter or click OK.
    • Confirm the User Account Control prompt if it appears.
  2. Navigate to the Security Subkey:
    • In the Registry Editor, navigate to the following path:
      Computer\HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Security
      (Note: 16.0 is for Outlook 2016, 2019, and Microsoft 365. If you have an older version, this number might be different, e.g., 15.0 for Outlook 2013, 14.0 for Outlook 2010.)
  3. Create a New Registry Value:
    • Right-click on the Security folder.
    • Select New > DWORD (32-bit) Value.
    • Name the new value EnableUnsafeClientMailRules.
  4. Modify the Value Data:
    • Double-click EnableUnsafeClientMailRules.
    • In the "Value data" field, enter 1.
    • Click OK.
  5. Restart Outlook:
    • Close and then reopen Outlook for the changes to take effect.

Once these steps are completed, the "run a script" option will become available when creating or modifying Outlook rules.

2. Create Your VBA Script (Macro)

Outlook rules execute VBA macros. You'll need to write your script within Outlook's VBA editor.

  1. Open VBA Editor:

    • In Outlook, press Alt + F11 to open the Microsoft Visual Basic for Applications window.
  2. Insert a New Module:

    • In the VBA Project pane (usually on the left), expand Project1 (VbaProject.OTM).
    • Right-click on Microsoft Outlook Objects (or Modules if it exists).
    • Select Insert > Module. A new module will appear in the Project pane.
  3. Write Your Script:

    • Double-click the newly created module (e.g., Module1) to open its code window.
    • Enter your VBA code. The script must be a Sub procedure that accepts an Outlook.MailItem object as an argument if it's meant to process an incoming email.

    Example: A Simple Script to Mark Mail as Read and Display a Message

    Public Sub ProcessNewMail(Item As Outlook.MailItem)
        ' Mark the email as read
        Item.UnRead = False
        Item.Save
    
        ' Display a custom message box
        MsgBox "New email from " & Item.SenderName & " processed by rule: " & Item.Subject, vbInformation, "Outlook Rule Script"
    
        ' Optional: Move the item to another folder
        ' Dim objFolder As Outlook.MAPIFolder
        ' Set objFolder = Application.GetNamespace("MAPI").Folders("YourMailboxName").Folders("Processed Emails")
        ' Item.Move objFolder
    End Sub
    • Important Notes:
      • The script should start with Public Sub YourScriptName(Item As Outlook.MailItem) for rules that trigger on new messages.
      • Always save your VBA project by clicking the disk icon or going to File > Save VbaProject.otm.

3. Create an Outlook Rule to Run the Script

Now, link your VBA script to an Outlook rule.

  1. Open Rules and Alerts:
    • In Outlook, go to File > Info > Manage Rules & Alerts.
  2. Create a New Rule:
    • In the Rules and Alerts dialog box, click New Rule....
    • Select Apply rule on messages I receive (or messages I send if applicable) and click Next.
  3. Set Conditions:
    • Choose the conditions that must be met for the rule to apply (e.g., from people or public group, with specific words in the subject).
    • Click on the underlined values to specify details (e.g., sender's email, subject keywords).
    • Click Next.
  4. Choose the Action "Run a Script":
    • In the "What do you want to do with the message?" step, check the box next to run a script.
    • Click on the underlined a script link.
    • A dialog box will appear listing your available macros. Select the Sub procedure you created (e.g., ProcessNewMail).
    • Click OK, then Next.
  5. Add Exceptions (Optional):
    • If there are any exceptions to the rule, select them here. Click Next.
  6. Finish Rule Setup:
    • Give your rule a descriptive name.
    • Ensure Turn on this rule is checked.
    • Click Finish.

Your Outlook rule is now active and will execute the specified VBA script whenever an incoming email meets the defined conditions.

Security Considerations

While powerful, running scripts from rules carries security risks. Malicious scripts could potentially harm your system or compromise data.

  • Trust only known sources: Only run scripts you have written yourself or obtained from trusted, verified sources.
  • VBA Project Signing: For enhanced security, you can digitally sign your VBA project. This allows you to set macro security levels to only trust signed macros, adding a layer of protection against unsigned, potentially malicious scripts.
  • Macro Security Settings: Outlook's Trust Center (File > Options > Trust Center > Trust Center Settings > Macro Settings) allows you to manage macro execution behavior. Be cautious about enabling all macros without signing.

By following these steps, you can leverage the power of custom scripts to automate and enhance your Outlook workflow.