Outlook IF Statement Examples for VBA and Mail Merge

VBA & macros · Updated November 3, 2026

Outlook IF Statement Examples for VBA and Mail Merge

Outlook does not have one universal IF formula: use Outlook rules for basic message conditions, VBA If...Then statements for custom logic in classic Outlook for Windows, or Word mail-merge IF fields for personalized email text. The right example depends on whether you want to sort messages, run a custom action, or change what each recipient sees.

What an IF statement means in Outlook

An IF statement checks whether something is true and then chooses what to do. In programming, the pattern is “if this condition is true, do one thing; otherwise, do another.” Outlook has several features that can produce similar results, but they are not interchangeable and do not all use the words IF in their interface.

For example, you might want to move messages from a particular sender, highlight emails with a certain subject, or include a different paragraph for each mail-merge recipient. These call for different tools:

  • Outlook rules: Apply built-in conditions and actions to incoming or existing messages.
  • VBA: Write custom conditional logic in classic Outlook for Windows.
  • Word mail merge: Insert an IF field to vary text in a personalized email.
  • Conditional formatting: Change how matching messages look in the message list; it does not perform an action on them.

So if you searched for an Outlook IF formula, first decide what result you need. Outlook does not provide a general-purpose formula bar for email, and typing an Excel-style formula into a message or rule will not make it run.

Use Outlook rules for common message conditions

A rule is usually the simplest option when the condition concerns ordinary message details such as the sender, subject words, recipient, or whether a message has an attachment. Rules combine conditions, actions, and optional exceptions. For example, you can make a rule that moves messages with “Project Atlas” in the subject to a folder, or assigns a category to mail from a particular person.

In classic Outlook for Microsoft 365 on Windows, open File > Manage Rules & Alerts, select New Rule, and follow the Rule Wizard. The wizard lets you select conditions and actions from lists; you do not write an IF expression. In new Outlook for Windows, open Settings > Mail > Rules to create or manage supported rules. The available options can differ between classic and new Outlook, and between account types.

Think of a basic rule as “if the selected conditions match, perform these actions.” For instance:

  • If the message is from a named sender, move it to a folder.
  • If the subject contains “schedule,” assign a category.
  • If the message is sent only to you, mark it for follow-up.

Rules are not a substitute for arbitrary logic. You cannot use them like a spreadsheet formula to calculate a value from message text, compare two extracted numbers, or build complex nested branches. If you need a condition the wizard does not offer, consider VBA, a supported Power Automate flow, or a different email process.

Outlook VBA IF statement example

VBA is the closest match to a traditional programming IF statement inside Outlook, but it works only in classic Outlook for Windows. New Outlook for Windows does not support Outlook VBA macros, and Outlook for Mac does not provide this VBA environment. The following macro checks the subject of the first selected message and displays a different response depending on its contents. It reads the message but does not change it.

Public Sub CheckSelectedMessage()
    Dim selectedItems As Outlook.Selection
    Dim candidate As Object
    Dim mail As Outlook.MailItem

    ' Get the items selected in the open Outlook window.
    Set selectedItems = Application.ActiveExplorer.Selection

    If selectedItems.Count = 0 Then
        MsgBox "Select a message first."
        Exit Sub
    End If

    Set candidate = selectedItems.Item(1)

    If TypeOf candidate Is Outlook.MailItem Then
        Set mail = candidate

        If InStr(1, mail.Subject, "invoice", vbTextCompare) > 0 Then
            MsgBox "This subject contains invoice."
        ElseIf InStr(1, mail.Subject, "receipt", vbTextCompare) > 0 Then
            MsgBox "This subject contains receipt."
        Else
            MsgBox "Neither word was found in the subject."
        End If
    Else
        MsgBox "The selected item is not an email message."
    End If
End Sub

The outer IF checks whether anything is selected. The next condition uses TypeOf so the code does not treat an appointment or another Outlook item as an email. The nested If...ElseIf...Else checks for two words. InStr returns a position when it finds a match and zero when it does not; vbTextCompare makes the search case-insensitive. The > 0 test turns that result into a true-or-false condition.

To try it, open classic Outlook, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, and paste the procedure into the module. Return to Outlook, select a message, and run the macro from the Macros dialog. Macro access may be restricted by your organization; do not lower security settings or run code from an untrusted source just to make a sample work.

Another VBA example: check for an attachment

Here is a smaller condition you can adapt inside the message-checking procedure above. It tests whether the selected email has at least one attachment. The example only reports the result, so it will not mark, move, or delete anything.

If mail.Attachments.Count > 0 Then
    MsgBox "This message has one or more attachments."
Else
    MsgBox "This message has no attachments."
End If

In this example, mail must already refer to a valid Outlook.MailItem, as it does after the type check in the first macro. You can place a different action inside either branch, such as assigning a category or saving an attachment, but check the effect carefully before automating it. A macro that changes or moves messages may affect the mailbox immediately, and Outlook macros cannot be undone with Ctrl+Z. Save a copy of important data before running a macro that modifies items.

Use an IF field in a Word email mail merge

If your goal is to send different wording to different recipients, use Word’s mail-merge rules rather than an Outlook inbox rule. For example, a customer in the West region might receive a regional event invitation while other recipients receive a general update. The conditional text belongs in the Word document used for the merge.

In Word for Microsoft 365, start an email merge from Mailings > Start Mail Merge > E-mail Messages, select a recipient list, then use Mailings > Rules > If…Then…Else to create a condition. You can also work with the field code directly. A simplified example is:

{ IF { MERGEFIELD Region } = "West" "Your western update is ready." "Your general update is ready." }

The braces shown here are Word field braces. Do not type ordinary curly braces and expect them to act as fields: in Word, insert field braces with Ctrl+F9 on Windows, then place the field code between them. The inner MERGEFIELD supplies each recipient’s Region value. During the merge, Word checks the comparison and inserts the matching sentence.

Preview several records before sending; a blank or inconsistently spelled Region value may send a recipient down the other branch. When the document is ready, use Mailings > Finish & Merge > Send E-Mail Messages and select the recipient field, subject line, and mail format. Email merge relies on a compatible desktop email setup, commonly classic Outlook on Windows. New Outlook is not a direct replacement for every classic Outlook mail-merge workflow, so confirm support in your current setup before preparing a large send.

Conditional formatting is visual, not an IF action

Classic Outlook for Windows also lets you emphasize messages that meet criteria. In a mail list, open View > View Settings > Conditional Formatting to create a view rule with formatting such as a font or color. This can make unread messages from a key client easier to spot, for example.

Conditional formatting changes how matching items appear in that view. It does not move a message, send a reply, change the message body, or decide which mail-merge sentence to use. If the goal is to automatically file mail, create an Outlook rule. If the goal is to create personalized wording, use a Word merge field. Feature availability and menu details can vary in new Outlook and on Mac, so the classic Outlook path above should not be assumed to match those apps.

How to choose the right approach

Match the tool to the job before writing code or building a workflow:

  • Sort or label incoming email: Start with an Outlook rule. It is easier to maintain than a macro for standard sender, subject, and recipient conditions.
  • Make a custom decision while working with an email: Use VBA if you have classic Outlook for Windows and the condition or action is not covered by rules.
  • Personalize a batch of outgoing messages: Build a Word mail merge and use an IF field for recipient-specific text.
  • Make matching messages stand out: Use conditional formatting in a supported Outlook view.
  • Automate a cloud mailbox workflow: Check whether Power Automate supports the trigger and actions you need for your account. It is a separate workflow tool, not an Outlook VBA IF statement, and connector availability can depend on your work or school setup and licensing.

For routine mail handling, prefer a built-in rule because it is visible in Outlook and does not require users to enable a macro. Reserve VBA for cases where its extra flexibility is genuinely useful. For merge messages, test a small preview and verify the recipient list and conditional text before sending.

Common problems and safety checks

A frequent mistake is to search for an IF formula in the Outlook Rules Wizard. The wizard offers predefined conditions, not a general coding editor. Another is to expect a macro to run in new Outlook or Outlook for Mac; the VBA examples above require classic Outlook for Windows. If the Visual Basic Editor or macro options are unavailable, an organizational policy may also be blocking them.

Text comparisons need care. A search for “invoice” in the sample checks the subject only, not the message body, sender name, or attachment contents. A Word merge comparison also depends on the exact values in the source list: “West,” “west,” and a blank cell may not behave as you intended. Clean the source data and preview representative records.

Finally, treat macros as executable code. Only use code you understand and trust, and follow your organization’s security rules. A harmless message-box example is a safer starting point than code that deletes, forwards, or moves messages. If you later add such actions, test on disposable sample messages and keep a recoverable copy of important data.

Frequently Asked Questions

Can I use an IF formula directly in Outlook?
Not as a general Outlook formula. Use a built-in rule for ordinary mail conditions, VBA in classic Outlook for custom program logic, or an IF field in Word when personalizing a mail merge.

Does Outlook have an IF function in the Rules Wizard?
No. The Rules Wizard lets you select conditions, actions, and exceptions, but it does not provide a general IF-function editor. Its options are designed for common email-handling tasks.

Do Outlook VBA IF examples work in new Outlook?
No. Outlook VBA macros require classic Outlook for Windows. New Outlook for Windows and Outlook for Mac do not run these Outlook VBA macros.

Can an Outlook rule check whether an email has an attachment?
Yes, supported Outlook rule wizards include an attachment-related condition. The exact wording and availability can vary by Outlook version and account, so check the condition list in your app.

Can a Word IF field personalize an email sent through Outlook?
Yes. A Word mail merge can evaluate recipient-list fields and insert different text, then send the completed messages through a compatible desktop email setup. Preview the results and confirm your Outlook version supports the workflow before sending.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.