Search code examples
vbaoutlookaccountrules

How to handle Account Condition Properties Rules in Outlook VBA


I am trying to add a "from account" condition to a set of Outlook Rules, which are created by VBA. The DisplayName of the account is: abcd@abcd.com, AccountType is: 0 and Class is: 105.

Dim oAccountRuleConditionSubscribe As Outlook.AccountRuleCondition
Dim oRuleNew As Outlook.Rule

Set oAccountRuleConditionSubscribe = oRuleNew.Conditions.Account
    With oAccountRuleConditionSubscribe
    .Enabled = True
    .Account.DisplayName = abcd@abcd.com
    End With

The above is the latest I could come up with, and still it will not take abcd@abcd.com as a valid account reference. I have exhausted all tutorials, glossaries and MSDN resources, and I would really appreciate your help.

I found a workaround, thanks to Eugene, with:

Dim oAccountRuleConditionSubscribe As Outlook.AccountRuleCondition
Dim oRuleNew As Outlook.Rule
Dim OutApp As Outlook.Application
Set OutApp = CreateObject("Outlook.Application")

    Set oAccountRuleConditionSubscribe = oRuleNew.Conditions.Account
    With oAccountRuleConditionSubscribe
        .Enabled = True
        .Account = OutApp.Session.Accounts.item(2)
    End With

But I am still struggling ot identigy the account by its DisplayName.

Any pointers?


Solution

  • Try quotes.

    Account.DisplayName Property (Outlook)

    "Returns a String representing the display name of the e-mail Account. Read-only."

    .Account.DisplayName = "abcd@abcd.com"
    

    Edit 2015 02 15

    If you want to use Account.DisplayName somewhere, it is as a string in read mode.

    This in addition to Eugene's "It seems you need to choose the account from the Accounts collection with the specified email address and then set it to the rule conditions." leads to setting/identifying the account outside of the With.

    Without saying this code can be used in a Rules situation, it would be something like:

    For Each olAcc In Accounts
        If olAcc.DisplayName = "abcd@abcd.com" then
            ' some rules code here
            Exit For
        End if
    Next olAcc
    

    Edit 2015 02 15 - End