Process messages received on a day of the week

Last reviewed on December 3, 2013

Run a script rule

To use the code, you'll create a rule with the desired conditions and choose 'run a script' as the only action, selecting this script.


Sub KeepFriday(Item As Outlook.MailItem)
datefri = WeekdayName(Weekday(aItem.ReceivedTime))

If datefri = "Friday" Then

'moves to the Friday subfolder under Inbox. 
   Item.Move Session.GetDefaultFolder(olFolderInbox).Folders("Friday")
Else 
   Item.Delete
End If


End Sub

VBA to run anytime

To use this code sample select the folder then run the macro


Sub KeepFridayOnly()
Dim dest As Outlook.MAPIFolder
Dim aItem As Object
Dim datefri As String

Set mail = Application.ActiveExplorer.CurrentFolder

For Each aItem In mail.Items

datefri = WeekdayName(Weekday(aItem.ReceivedTime))

If datefri = "Friday" Then
   aItem.Move Session.GetDefaultFolder(olFolderInbox).Folders("Friday")
End If

Next aItem

 Set aItem = Nothing
 Set myolApp = Nothing
 
End Sub


Process mail by date and time

Outlook's Rules and Search functions can't search by times. While you can "do something" with messages (or any Outlook item) between two dates, you can't filter by time too. However, you can use VBA to "do something" messages that fall within a certain time period.

In this code sample, I'm adding a category to messages that arrived after 6 PM during the last 30 days. This macro runs on the messages in the selected folder.

Sub FlagByTime() 
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 - 30 Then
 
strTime = TimeValue(aItem.ReceivedTime) 

'Check the received time
  If strTime > #6:00:00 PM# Then
     aItem.Categories = "Nighttime"
     aItem.Save
 End If
 
 End If
 
Next aItem
 
 Set aItem = Nothing
End Sub

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

If the Post Coment button disappears, press your Tab key.