How to Create an Outlook Rule That Excludes Emails With Specific Header
🔍 WiseChecker

How to Create an Outlook Rule That Excludes Emails With Specific Header

You want an Outlook rule that runs on all incoming messages except those that contain a specific header field, such as X-Category or X-Priority. Outlook’s Rules Wizard does not expose a native condition for custom header fields. This article explains how to use a combination of a run-a-script rule and a short VBA macro to exclude messages that contain a particular header value. You will learn the exact steps to write the macro, attach it to a rule, and test the exclusion.

Key Takeaways: Exclude Emails by Custom Header in Outlook

  • Developer tab > Visual Basic > Insert > Module: Create a VBA function that checks a specific header field and returns True if the header is absent.
  • Rules Wizard > Run a script > Select your macro: Attach the macro to a rule so the rule only processes messages that lack the target header.
  • File > Options > Trust Center > Macro Settings > Enable all macros: Required before the rule can execute the VBA code.

ADVERTISEMENT

How Outlook Rules Handle Custom Headers

Outlook’s built-in rule conditions cover sender, recipient, subject, body, and a set of predefined header fields such as Importance or Sensitivity. The Rules Wizard does not include a condition for arbitrary header fields like X-Mailer, X-List, or custom X-headers added by an email system. To work around this limitation, you must use the Run a script action, which calls a VBA function that inspects the MailItem.PropertyAccessor object. The macro can read any MAPI property or transport header. By writing a function that returns True only when the header is missing, the rule effectively excludes messages that contain the header. The macro does not modify the message; it only controls whether the rule continues to its remaining actions.

Prerequisites for Using the Run a Script Rule Action

Before you create the rule, confirm the following items are in place.

  • Outlook for Windows desktop client. The Run a script action is not available in Outlook on the web or Outlook for Mac.
  • Macro security set to allow signed or all macros. You will enable this in the Trust Center.
  • The exact header name you want to exclude. Header names are case-insensitive but must be spelled exactly as they appear in the message source, for example X-Custom-Header.
  • A test email that contains the header so you can verify the exclusion works.

ADVERTISEMENT

Steps to Create the VBA Macro That Checks a Header

  1. Open the VBA editor
    In Outlook, press Alt+F11. This opens the Microsoft Visual Basic for Applications window.
  2. Insert a new module
    In the Project pane on the left, expand Project1 and right-click Modules. Select Insert > Module. A blank code window appears.
  3. Paste the header-check function
    Copy and paste the following code into the module window. Replace X-Your-Header with the actual header name you want to exclude.
    Function ExcludeIfHeaderPresent(MyItem As MailItem) As Boolean
        Const HEADER_NAME As String = "X-Your-Header"
        Dim headerValue As String
        On Error Resume Next
        headerValue = MyItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/" & HEADER_NAME & "/0x0000001F")
        On Error GoTo 0
        ' Return True (run the rule) only if the header is missing
        ExcludeIfHeaderPresent = (headerValue = "" Or IsNull(headerValue))
    End Function
    

    The function reads the named MAPI property that corresponds to the transport header. If the header is absent, the function returns True, which tells the rule to continue processing. If the header exists, the function returns False and the rule stops.

  4. Save the macro and close the editor
    Press Ctrl+S to save. Name the project if prompted. Close the VBA window and return to Outlook.

Steps to Enable Macros in the Trust Center

  1. Open Trust Center settings
    Go to File > Options > Trust Center > Trust Center Settings.
  2. Change macro settings
    Select Macro Settings. Choose Enable all macros. This setting allows the rule to run the VBA function without a security prompt each time. Click OK twice to close both dialog boxes.

Steps to Create the Rule That Uses the Macro

  1. Open the Rules Wizard
    Go to File > Manage Rules & Alerts. In the Rules and Alerts dialog, click New Rule.
  2. Select a template
    Under Start from a blank rule, select Apply rule on messages I receive. Click Next.
  3. Add conditions (optional)
    Select any conditions you want to apply to all messages, such as from a specific sender or with specific words in the subject. These conditions run before the macro check. Click Next. If prompted, click Yes to apply the rule to all messages.
  4. Add the Run a script action
    In the Select action(s) list, scroll down and check run a script. In the Step 2 box, click the underlined word script. In the Choose a Script dialog, select the function ExcludeIfHeaderPresent from the list. Click OK.
  5. Add the main action
    Still in the Select action(s) list, check the action you want to apply to messages that pass the header check. For example, move it to a folder, assign a category, or forward it. Click Next.
  6. Set exceptions (optional)
    Add any exceptions. Click Next.
  7. Name the rule and finish
    Type a descriptive name such as “Exclude X-Header Messages”. Check Turn on this rule. Click Finish, then click OK to close the Rules and Alerts dialog.

Testing the Rule

Send yourself an email that contains the target header. To add a header in a test message, you can use a tool like Telnet or a script, or ask your email administrator to send one. The message should be delivered to your Inbox without the rule action being applied. A second message that lacks the header should trigger the rule action. If the rule does not run, check the macro security level and confirm the VBA function compiles without errors by opening the VBA editor and clicking Debug > Compile VBAProject.

If the Rule Does Not Exclude Messages Correctly

Outlook shows a security prompt every time the rule runs

The macro accesses the PropertyAccessor object, which triggers Outlook’s object model guard in some security configurations. To suppress prompts, install the Outlook Security Manager add-in from a trusted vendor, or use a third-party tool that registers the macro as trusted. Alternatively, use a digital certificate to sign the VBA project and enable only signed macros in the Trust Center.

The macro returns an error or does not detect the header

Verify the header name is correct and that it is a transport header visible in the message source. Open the test message, press Ctrl+F3 to open the Properties dialog, and look for the header in the Internet Headers box. The header name in the VBA code must match exactly except for case. Also confirm the schema string in the GetProperty call uses the correct GUID for named properties. The GUID in the code above is the standard one for string-named properties. If your header uses a different namespace, adjust the schema string accordingly.

The rule runs on all messages regardless of the header

The function returns True when the header is missing. If the rule still processes messages that contain the header, the function may be returning True incorrectly. Add a debug message in the VBA editor by inserting MsgBox headerValue before the return line, send a test message, and observe the dialog. If the dialog shows a value, the header is present and the function should return False. Correct the logic if needed.

Run a Script Rule vs Built-in Conditions: Key Differences

Item Run a Script Rule Built-in Conditions
Header support Any transport header via PropertyAccessor Only Importance, Sensitivity, and a few predefined headers
Setup effort Requires VBA code and macro enablement Point-and-click from the Rules Wizard
Security prompts Possible with PropertyAccessor access No prompts
Portability Macro must be present on each machine Rules sync across Exchange accounts

You can now create an Outlook rule that excludes emails with a specific header by using a VBA macro attached to the Run a script action. Test the rule with a message that contains the header to confirm the exclusion works. For a more permanent solution without macros, consider using a transport rule on the Exchange server if you have administrator access. The transport rule can filter on any header before the message reaches the Inbox, eliminating the need for client-side VBA code.

ADVERTISEMENT