VBA to run rules on messages within the last 2 weeks in Outlook 2013

141 views Asked by At

I have millions of messages dating back to the first day of employment

When I implement this code

Session.DefaultStore.GetRules.Item("myRuleName").Execute

It executes it on all messages.

However I am only interested in executing this rule on messages within the last two weeks and only on a mailbox (shared) called "[email protected]"

We are using Outlook 2013 with Exchange


I know how to get the current item(s) but how do I apply my rules to those specific mail items

 Sub MailItemByTime() 
 Dim aItem As Object
 Dim strTime As String

 Set mail = Application.ActiveExplorer.CurrentFolder
 For Each aItem In mail.Items

      'Check the message age
      If aItem.ReceivedTime > Date - 14 Then

      ' How to process these specific items ONLY????

 End If

 Next aItem

 Set aItem = Nothing
End Sub
1

There are 1 answers

1
Eugene Astafiev On

You can call a VBA sub from a rule where you may check out the target Outlook message (when it was received and etc.). The sub should be in the following format:

public sub Test(mail as MailItem)
    ' check out the mail object 
end sub 

You may find the ReceivedTime property of the MailItem class helpful. It returns a Date indicating the date and time at which the item was received.