• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Process messages received on a day of the week

Slipstick Systems

› Developer › Process messages received on a day of the week

Last reviewed on November 4, 2019     4 Comments

A security update disabled the Run a script option in the rules wizard in Outlook 2010 and all newer Outlook versions. See Run-a-Script Rules Missing in Outlook for more information and the registry key to fix restore it.

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

ItemAdd Macro

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

 

Add a Category to messages received at specific dates and times

This macro will add a category to messages received at specific times between specific dates. In this example, messages received between 8 AM and 10 AM from 10/20/2019 through 11/3/2019.

categorise messages received 8 - 10 am am

This macro runs on the selected folder.

Public Sub FindMailbyTime()
    Dim objOL As Outlook.Application
    Dim objItems As Outlook.Items
    Dim objFolder As Outlook.MAPIFolder
    Dim obj As Object
 
    Set objOL = Outlook.Application
    Set objFolder = objOL.ActiveExplorer.CurrentFolder
    Set objItems = objFolder.Items
    
    For Each obj In objItems
    If obj.MessageClass = "IPM.Note" Then
    If obj.ReceivedTime => DateValue("10/20/2019") And obj.ReceivedTime <= DateValue("11/3/2019") Then
' 24-hour format
    If Hour(obj.ReceivedTime) >= 8 And Hour(obj.ReceivedTime) < 10 Then
     
     With obj
    ' do whatever
       Debug.Print .ReceivedTime, .Subject
     .Categories = "Received 8 - 10"
     .Save
     End With
     
    End If
    End If
    End If
    
    Next
 
    Set obj = Nothing
    Set objItems = Nothing
    Set objFolder = Nothing
    Set objOL = 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:

  1. Expand Project1 and double click on ThisOutlookSession.
  2. 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.

VBA Date and Time Functions

FunctionDescription
NowCurrent date and time.
Example: 7/27/19 11:32:18 AM returned by Now
Today()Current date only
DateCurrent date only. Example:
7/27/19 returned by Date
TimeCurrent time only. Example:
11:32:18 AM returned by Time
TimeValue()Time part of argument.
Example: 11:32:18 AM returned by TimeValue(Now)
DateValue()Date part of argument
(excellent for ordering by date)
DateSerial()Date part of three arguments: year, month, day
DateSerial(Year(Now), Month(Now)-1, Day(Now))
DatePart()Returns a portion of the date.
Year example: 2019 returned by DatePart("yyyy", Date)
Month example: 7 returned by DatePart("m", #7/27/2019#)
Week of year example: 30 returned by DatePart("ww", #7/27/2019#)
DatePart function
Year()Returns the year portion of the date argument.
Month()Returns the month portion of the date argument.
Day()Returns the day portion of the date argument.
MonthName()Used to format month names.
July returned by MonthName(Month(Date))
WeekdayName()Used to format day names.
Wednesday returned by WeekdayName(Weekday(Date))
DateDiff()Returns the difference in dates.
Days example: -237 returned by DateDiff("d", #7/27/2019#, #12/2/2018#)
Months example: 2 returned by DateDiff("m", #7/27/2019#, #9/14/2019#)
0 is returned if the two dates have same month and year
DateAdd()Add and subtract dates.
7/28/2019 returned by DateAdd("yyyy", 1, #7/27/2019#))
Today"s date + 30 days returned by DateAdd("d", 30, Date)
The date 45 days ago returned by DateAdd("d", -45, Date)
To find Monday of a week: DateAdd("d",-WeekDay(Date)+2,Date)
Format()Used for formatting dates. Examples:
Wed, 27-July-2019 returned by Format(Date,"ddd, d-mmmm-yyyy")
27-Jul-2019 returned by Format(Date,"d-mmm-yy")

More Information

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
Process messages received on a day of the week was last modified: November 4th, 2019 by Diane Poremsky

Related Posts:

  • Save all incoming messages to the hard drive
  • How to Process Mail After Business Hours
  • A simple run a rule script marks Outlook messages read when the messag
    Use a run a script rule to mark messages read
  • Move messages CC'd to an address

About 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.

Subscribe
Notify of
4 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Jordan (@guest_214848)
March 2, 2020 6:24 pm
#214848

Hi Diane,

I am trying to get the script to work within a rule but it is not working for me. I added the script function back to Outlook via a registry edit. I have Microsoft Outlook for Office 365 and latest 64-bit version of Outlook.

Nothing happens when I run my rule with the script. I am trying to get an email I receive on Tuesdays, instead of Fridays like your example, to move the email to my "Tuesday" labeled folder from my "OneCloud" labeled folder, both under my inbox, where I can then have another rule to forward the email to the Outlook contact group of my choosing. See attached screenshots. Can you please help me?

Thank you,

Rules.png.jpg
Outlook Script.png.jpg
0
0
Reply
MPP (@guest_213459)
June 17, 2019 7:28 am
#213459

When I try the code in the "Process mail by date and time" section I get the message:

"Run-time error '438':
Object doesn't support this property or method"

Please could someone tell me how to correct this?

0
0
Reply
Pietje (@guest_183216)
May 1, 2014 11:51 am
#183216

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.

0
0
Reply
Diane Poremsky (@guest_183217)
Reply to  Pietje
May 1, 2014 1:38 pm
#183217

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.

1
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 30 Issue 15

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Use Classic Outlook, not New Outlook
  • How to Remove the Primary Account from Outlook
  • Disable "Always ask before opening" Dialog
  • Adjusting Outlook's Zoom Setting in Email
  • How to Hide or Delete Outlook's Default Folders
  • This operation has been cancelled due to restrictions
  • Outlook SecureTemp Files Folder
  • Remove a password from an Outlook *.pst File
  • Reset the New Outlook Profile
  • Save Attachments to the Hard Drive
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
  • New Outlook: Show To, CC, BCC in Replies
  • Insert Word Document into Email using VBA
  • Delete Empty Folders using PowerShell
  • Warn Before Deleting a Contact
  • Classic Outlook is NOT Going Away in 2026
  • Use PowerShell to Delete Attachments
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

New Outlook: Show To, CC, BCC in Replies

Insert Word Document into Email using VBA

Delete Empty Folders using PowerShell

Warn Before Deleting a Contact

Classic Outlook is NOT Going Away in 2026

Use PowerShell to Delete Attachments

Newest Code Samples

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

Use PowerShell or VBA to get Outlook folder creation date

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2025 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment