0

Is there any way to know which MailItems satisfy a given Rule?

I mean to move a set of emails by applying a Rule (as a one-off operation), but then know which were these.

2 Answers2

1

You can use the Find/FindNext or Restrict methods to find Outlook items that satisfy to your conditions. Also you may find the AdvancedSearch method of the Application class helpful.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • At this point, I needed to check: 1) email subjects partially matching a given string (i.e., with wildcards), 2) email addresses with wildcards as well. This is what led me to asking about Rules instead of using Find/FindNext or Restrict. The AdvancedSearch is possibly not useful for me, as I think it cannot be taken advantage of with VBA. – sancho.s ReinstateMonicaCellio Mar 13 '15 at 10:00
1

You can either parse the rule conditions (Rule.Conditions) and programmatically check if they apply to a particular message, or you can call Rule.Execute and let it do what it needs to do. You can set up event handler on the target folder (Items.ItemAdd) ahead of time to figure out which items were moved to the target folder.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I understand the second option, I would try that. For the first option, I do not know how to "programmatically check" if a certain `Condition` applies to a given `MailItem`, could you clarify that? – sancho.s ReinstateMonicaCellio Mar 13 '15 at 01:42
  • For each possible condition (e.g. Rule.From.Enable == true), check the condition (loop through Rule.From.Recipients and check the email address of each Recipient against MailItem.SenderEmailAddress). – Dmitry Streblechenko Mar 13 '15 at 02:56
  • At this point, I needed to check: 1) email subjects partially matching a given string (i.e., with wildcards), 2) email addresses with wildcards as well. This is what led me to asking about `Rule`s instead of using `Find`/`FindNext` or `Restrict`. Can these conditions be programmatically checked as part of your proposal? – sancho.s ReinstateMonicaCellio Mar 13 '15 at 09:25
  • No, it would be your responsibility to explicitly check the conditions against every message. Or you can try to create a restriction from a rule and use it in Find/Restrict. – Dmitry Streblechenko Mar 13 '15 at 13:26