Last reviewed on December 3, 2013   —  2 Comments

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

Comments

  1. Pietje says

    Hi, I find it not usefull, cause the script can only run on a client and at the specific day i am not at work, thats why i neede the script.

    • Diane Poremsky says

      These scripts require outlook to be open to run, even one triggered by reminders or an email rule. If outlook is open you can use a task reminder set for that specific day, or if you want to remotely trigger it, send an email with a specific (and unique) subject line.

Leave a Reply

Please post long or more complicated questions at OutlookForums by Slipstick.com.

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