1

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:

  • colRules, objRuleSend are Outlook.Rules objects
  • objToCondition is an Outlook.ToOrFromRuleCondition
  • RuleName is a string with only letters in it
  • Address is a string variable with a name.name@company.com format.
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
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
JimG
  • 31
  • 5
  • The code is supposed to be laid out as code, but as usual, that feature didn't work. – JimG Jan 31 '18 at 21:01
  • So, Mat, how did you fix that? (Or, more importantly, how can I make it work when I create a post? I highlighted all the code and clicked the button with curly braces, but all it did was indent it. Same thing with ctrl-k. – JimG Jan 31 '18 at 21:36

2 Answers2

1

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

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Yeah, but that just move the mail. I want to create a rule for items sent to a person. – JimG Jan 31 '18 at 21:34
  • No, setting that properly will move the message *after* it is sent. – Dmitry Streblechenko Jan 31 '18 at 21:47
  • OK, I see that now. – JimG Jan 31 '18 at 21:49
  • However, I really want to use a rule. I work in a help desk, and when I get a ticket, I create a folder for the person's emails back-and-forth to me. With this solution, I would have to change the code when a ticket is opened and again when closed. What I want to do is create two rules (one for receiving, one for sending) upon first send or receive to a customer, and then delete them both once the ticket is resolved. – JimG Jan 31 '18 at 21:52
  • Oh, and I already have the rule-creation macro working for items I receive from my customers. – JimG Jan 31 '18 at 21:54
  • I don't think you can - a send rule only lets you copy to a specified folder. Is writing a short VBA macro an option? – Dmitry Streblechenko Jan 31 '18 at 22:52
  • It could be an option, but it leaves me having to rewrite the code every time I add or remove a customer's address. – JimG Feb 01 '18 at 18:15
  • However, I'm going to mark this as the answer, because I only need the "send" rule one time: when I first open the conversation. However, I can't create the rule before that, because I don't know the customer's address. Thus, I don't need to make a send rule after all. – JimG Feb 01 '18 at 18:16
0

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
Ruut
  • 1,091
  • 1
  • 16
  • 29