0

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?

clippertm
  • 149
  • 5
  • 18
  • Where do you need to identify accounts? What exactly do you need to implement? 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. Am I right? – Eugene Astafiev Feb 13 '15 at 09:07
  • Yes, you are right. I would like to select it using DisplayName, not item(#). – clippertm Feb 16 '15 at 00:33
  • But I am still struggling ot identigy the account by its DisplayName. - What exactly are you looking for??? – Eugene Astafiev Feb 16 '15 at 14:05

3 Answers3

0

.Account.DisplayName = abcd@abcd.com

Instead, you need to set a valid account object (see Namespace.Accounts) to the Account property of the AccountRuleCondition class - an Account object that represents the account used to evaluate the rule condition.

See Specifying Rule Conditions for more information. Also you may find the How to: Create a Rule to Move Specific E-mails to a Folder article helpful.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
0

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

niton
  • 8,771
  • 21
  • 32
  • 52
  • Tried that before posting on stackoverflow. I get "Compile Error. Can't assign to read-only property". – clippertm Feb 16 '15 at 01:53
  • Seriously? Is there no way to have a simple, direct .Account.DisplayName = "abcd@abcd.com" equivalent? Why do I have to go list all accounts through a loop? – clippertm Feb 18 '15 at 02:45
0

I was having the same problem and found the solution.

Set oAccountRuleConditionSubscribe = oRuleNew.Conditions.Account
With oAccountRuleConditionSubscribe
  .Enabled = True
  Set .Account = OutApp.Session.Accounts.item("abc@def.com")
End With
Paul Roub
  • 36,322
  • 27
  • 84
  • 93