You want to automate a custom action in Outlook, such as moving emails to a specific folder based on complex logic or automatically saving attachments. The built-in rule conditions and actions may not cover your exact needs. Outlook rules can run a script action, but this feature is only available in the classic version of Outlook for Windows, not in the new Outlook for Windows or Outlook on the web. This article explains how to enable the Run a Script action in a rule and how to write and assign a simple VBA script to automate a task.
Key Takeaways: Creating an Outlook Rule That Runs a VBA Script
- File > Options > Trust Center > Trust Center Settings > Macro Settings: Enable all macros to allow VBA scripts to run when triggered by a rule.
- Developer tab > Visual Basic: Open the VBA editor and insert a new module to write your script for the rule action.
- File > Manage Rules & Alerts > New Rule > Start from a Blank Rule > Check messages > Select action Run a Script: Assign your VBA script as the rule action.
Why the Run a Script Action Is Limited to Classic Outlook
The Run a Script action is part of the legacy client-side rule engine in Outlook. This engine runs on your local computer and can execute VBA macros stored in the Outlook VBA project. The new Outlook for Windows and Outlook on the web use a server-side rule engine that does not support executing local scripts. Classic Outlook (the full desktop application) retains this feature for backward compatibility with business workflows that rely on custom automation.
The script action runs after the rule conditions are met. The script must be a VBA subroutine in the Outlook VBA project that accepts a single argument of type MailItem. The script can read or modify the mail item but cannot create new items or show dialog boxes during rule execution. If your script requires user input or creates new items, it will fail silently.
Steps to Set Up an Outlook Rule That Runs a Script
- Enable macros in Outlook Trust Center
Open Outlook. Go to File > Options > Trust Center. Click Trust Center Settings. Select Macro Settings. Choose Enable all macros. Click OK twice to close the dialogs. - Open the VBA editor
Press Alt + F11 to open the Visual Basic for Applications editor. If the Project Explorer pane is not visible, press Ctrl + R to show it. - Insert a new module
In the Project Explorer, locate Project1 (VbaProject.OTM). Right-click it and select Insert > Module. A blank code window opens. - Write the script subroutine
Paste the following example script into the module window. This script moves messages from a specific sender to a folder named “Project X”. Replace the sender email and folder name as needed.Public Sub MoveToProjectX(ByVal myMail As Outlook.MailItem) Dim objFolder As Outlook.Folder Set objFolder = Application.Session.GetDefaultFolder(olFolderInbox).Folders("Project X") myMail.Move objFolder Set objFolder = Nothing End Sub - Save the script
Press Ctrl + S to save the VBA project. Close the VBA editor. - Create a new rule
Go to File > Manage Rules & Alerts. Click New Rule. Under Start from a Blank Rule, select Check messages when they arrive. Click Next. - Set conditions
Select the conditions you want. For example, check from people or public group and then click the underlined value in the rule description to specify the sender. Click Next. - Assign the script action
In the list of actions, scroll down and check run a script. In the rule description area, click the underlined word script. A dialog shows a list of available scripts. Select MoveToProjectX and click OK. Click Next. - Set exceptions and name the rule
Add any exceptions if needed. Click Next. Enter a name for the rule, such as “Move Project X Emails”. Ensure Turn on this rule is checked. Click Finish.
Common Issues When Running a Script Rule
“The script action is not available” message
This error appears when the VBA project does not contain a public subroutine with the correct signature. The subroutine must be Public and accept exactly one argument of type Outlook.MailItem. If the subroutine is Private or has a different argument type, the rule editor will not list it. Open the VBA editor and verify the subroutine declaration matches the example above.
Script runs but does nothing
The script may contain a runtime error that is silently ignored. Test the script manually by running it from the VBA editor with a sample mail item. In the Immediate window, type ?Application.Session.GetDefaultFolder(olFolderInbox).Folders("Project X") to confirm the folder exists. If the folder name is misspelled, the script will fail silently. Also ensure the macro security setting is not blocking execution. Set macro security to Enable all macros as shown in step 1.
Rule runs but script does not trigger
The rule engine may not trigger client-side actions if Outlook is running in cached mode with a slow connection. Check that Outlook is online. Go to Send/Receive tab and click Work Offline to toggle it off if it is on. Also verify that the rule is not set to run only on this computer and that the script is saved in the correct VBA project. The script must be in Project1 (VbaProject.OTM), not in an add-in project.
| Item | Classic Outlook | New Outlook / Outlook Web |
|---|---|---|
| Rule type | Client-side and server-side | Server-side only |
| Run a Script action | Available | Not available |
| VBA macro requirement | Required | Not supported |
| Best for | Custom automation on local machine | Basic rules that work everywhere |
The Run a Script action in classic Outlook gives you the ability to automate tasks that standard rules cannot handle, such as moving emails based on custom logic or modifying message properties. Before creating a script rule, confirm that you are using classic Outlook for Windows and that macros are enabled. Write a simple test script first, then assign it to a rule with a single condition. This approach reduces troubleshooting time and helps you learn the exact script signature required. For advanced automation, consider using Power Automate with Outlook connectors, which works across all Outlook versions and does not require VBA.