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
This version of the macro is an ItemAdd macro. This means it run when a message is added to the folder the macro is watching. In this sample, we're using Select Case to check the day of the week and assign a different category based on the date.
This macro is added to ThisOutlookSession and runs when Outlook starts. To test it without restarting Outlook, click in the Application_Startup macro and click Run. Select one or two messages, then Ctrl+C, V to copy and paste them in place. The macro will run on the copies.
Option Explicit Private WithEvents olInboxItems As Items Private Sub Application_Startup() Dim objNS As NameSpace Set objNS = Application.Session ' instantiate objects declared WithEvents Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items Set objNS = Nothing End Sub Private Sub olInboxItems_ItemAdd(ByVal Item As Object) Dim dayname As String Dim strcat As String dayname = WeekdayName(Weekday(Item.ReceivedTime)) Select Case dayname Case "Monday", "Tuesday" strcat = "Due Friday" Case "Wednesday", "Thursday" strcat = "Due Monday" Case "Friday", "Saturday" strcat = "Due Tuesday" Case "Sunday" strcat = "Due Wednesday" End Select Item.Categories = strcat Item.Save 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
How to use the macro
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security. If Outlook tells you it needs to be restarted, close and reopen Outlook. Note: after you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Now open the VBA Editor by pressing Alt+F11 on your keyboard.
To use the macro code in ThisOutlookSession:
- Expand Project1 and double click on ThisOutlookSession.
- Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
Application_Startup macros run when Outlook starts. If you are using an Application_Startup macro you can test the macro without restarting Outlook by clicking in the first line of the Application_Startup macro then clicking the Run button on the toolbar or pressing F8.
More information as well as screenshots are at How to use the VBA Editor.
Date and Time functions
More Run a Script Samples:
- Autoaccept a Meeting Request using Rules
- Automatically Add a Category to Accepted Meetings
- Blocking Mail From New Top-Level Domains
- Convert RTF Messages to Plain Text Format
- Create a rule to delete mail after a number of days
- Create a Task from an Email using a Rule
- Create an Outlook Appointment from a Message
- Create Appointment From Email Automatically
- Delegates, Meeting Requests, and Rules
- Delete attachments from messages
- Forward meeting details to another address
- How to Change the Font used for Outlook's RSS Feeds
- How to Process Mail After Business Hours
- Keep Canceled Meetings on Outlook's Calendar
- Macro to Print Outlook email attachments as they arrive
- Move messages CC'd to an address
- Open All Hyperlinks in an Outlook Email Message
- Outlook AutoReplies: One Script, Many Responses
- Outlook's Rules and Alerts: Run a Script
- Read Outlook Messages using Plain Text
- Receive a Reminder When a Message Doesn't Arrive?
- Run a script rule: Autoreply using a template
- Run a Script Rule: Change Subject then Forward Message
- Run a script rule: Reply to a message
- Run a Script Rule: Send a New Message when a Message Arrives
- Run Rules Now using a Macro
- Run-a-Script Rules Missing in Outlook
- Save all incoming messages to the hard drive
- Save and Rename Outlook Email Attachments
- Save Attachments to the Hard Drive
- Save Outlook Email as a PDF
- Sort messages by Sender domain
- Talking Reminders
- To create a rule with wildcards