JimG
JimG

Reputation: 31

How can I use vba to create a rule to move sent items to folder?

In Outlook (2016 or 365), I would like to use VBA to create a rule that moves items I send to a specific person.

I've got most of the code done (lifted from a Microsoft code sample), but I can't figure out what the condition fields should get. I know it isn't right (there's no such thing as ".sender" in that type of object), but I don't know what I SHOULD put there. There are only seven properties on a ToOrFromRuleCondition (Application, Class, ConditionType, Enabled, Parent, Recipients, and Session), and none of them deal with the sender.

In the code below:

Set colRules = Application.Session.DefaultStore.GetRules()
Set objRuleSend = colRules.Create(RuleName & "Send", olRuleSend)

Set objToCondition = objRuleSend.Conditions.SentTo
With objToCondition
  .Enabled = True
  .Sender = Address ' <-- this is the line that fails.
  .Recipients.ResolveAll
End With

Upvotes: 1

Views: 739

Answers (2)

Ruut
Ruut

Reputation: 1161

This should work

Set colRules = Application.Session.DefaultStore.GetRules()
Set objRuleSend = colRules.Create(RuleName & "Send", olRuleSend)

Set objToCondition = objRuleSend.Conditions.SentTo
With objToCondition
  .Enabled = True
  .Recipients.Add Address ' <-- this is the line that is fixed.
  .Recipients.ResolveAll
End With

Upvotes: 0

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66255

You can intercept the Application.ItemSend event, check if the recipients are right, then set the MailItem.SaveSentMessageFolder property to the right folder.

Upvotes: 1

Related Questions