Process messages received on a day of the week

Chia sẻ bởi:hands
★★★★★
Quảng cáo

A security update disabled the Run a script option in the rules wizard in Outlook 2010 and all newer Outlook versions. See [URL='www.slipstick.com/outlook/rules/outlook-run-a-script-rules/']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.

cdn.slipstick.com/images/2012/developer/received8-10.png

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.Messaghocexcel = "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 [URL='www.slipstick.com/developer/how-to-use-outlooks-vba-editor/']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 [URL='www.slipstick.com/developer/how-to-use-outlooks-vba-editor/']How to use the VBA Editor.

VBA Date and Time Functions

Function
Description

Now
Current date and time.
Example: 7/27/19 11:32:18 AM returned by Now

Today()
Current date only

Date
Current date only. Example:
7/27/19 returned by Date

Time
Current 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#)
[URL='docs.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/datepart-function']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:

  • [URL='www.slipstick.com/outlook/calendar/autoaccept-a-meeting-request-using-rules/']Autoaccept a Meeting Request using Rules
  • [URL='www.slipstick.com/outlook/rules/add-category-accepted-meetings/']Automatically Add a Category to Accepted Meetings
  • [URL='www.slipstick.com/outlook/email/blocking-new-top-level-domains/']Blocking Mail From New Top-Level Domains
  • [URL='www.slipstick.com/developer/macro-convert-rtf-messages-plain-text-html-format/']Convert RTF Messages to Plain Text Format
  • [URL='www.slipstick.com/outlook/rules/create-rule-to-expire-mail-after-a-number-of-days/']Create a rule to delete mail after a number of days
  • [URL='www.slipstick.com/outlook/rules/create-task-email-rule/']Create a Task from an Email using a Rule
  • [URL='www.slipstick.com/developer/code-samples/create-outlook-appointment-from-message/']Create an Outlook Appointment from a Message
  • [URL='www.slipstick.com/developer/code-samples/create-appointment-email-automatically/']Create Appointment From Email Automatically
  • [URL='www.slipstick.com/outlook/rules/delegates-meeting-requests-rules/']Delegates, Meeting Requests, and Rules
  • [URL='www.slipstick.com/developer/code-samples/delete-attachments-messages/']Delete attachments from messages
  • [URL='www.slipstick.com/developer/forward-meeting-details-to-another-address/']Forward meeting details to another address
  • [URL='www.slipstick.com/outlook/rules/change-font-outlooks-rss-feeds/']How to Change the Font used for Outlook's RSS Feeds
  • [URL='www.slipstick.com/outlook/email/process-mail-business-hours/']How to Process Mail After Business Hours
  • [URL='www.slipstick.com/outlook/calendar/meeting-cancellation-calendar/']Keep Canceled Meetings on Outlook's Calendar
  • [URL='www.slipstick.com/developer/print-attachments-as-they-arrive/']Macro to Print Outlook email attachments as they arrive
  • [URL='www.slipstick.com/developer/move-messages-cc-address/']Move messages CC'd to an address
  • [URL='www.slipstick.com/developer/code-samples/open-hyperlinks-email-message/']Open All Hyperlinks in an Outlook Email Message
  • [URL='www.slipstick.com/developer/fun-arrays-one-macro-many-replies/']Outlook AutoReplies: One Script, Many Responses
  • [URL='www.slipstick.com/outlook/rules/outlooks-rules-and-alerts-run-a-script/']Outlook's Rules and Alerts: Run a Script
  • [URL='www.slipstick.com/outlook/email/read-outlook-messages-plain-text/']Read Outlook Messages using Plain Text
  • [URL='www.slipstick.com/outlook/rules/run-script-rule-watch-updates/']Receive a Reminder When a Message Doesn't Arrive?
  • [URL='www.slipstick.com/developer/run-a-script-rule-autoreply-using-a-template/']Run a script rule: Autoreply using a template
  • [URL='www.slipstick.com/outlook/rules/run-script-rule-change-subject-message/']Run a Script Rule: Change Subject then Forward Message
  • [URL='www.slipstick.com/outlook/rules/run-script-rule-reply-message/']Run a script rule: Reply to a message
  • [URL='www.slipstick.com/outlook/rules/send-a-new-message-when-a-message-arrives/']Run a Script Rule: Send a New Message when a Message Arrives
  • [URL='www.slipstick.com/outlook/rules/run-rules-now-using-macro/']Run Rules Now using a Macro
  • [URL='www.slipstick.com/outlook/rules/outlook-2016-run-a-script-rules/']Run-a-Script Rules Missing in Outlook
  • [URL='www.slipstick.com/outlook/archive-outlook/save-incoming-messages-hard-drive/']Save all incoming messages to the hard drive
  • [URL='www.slipstick.com/developer/code-samples/save-rename-outlook-email-attachments/']Save and Rename Outlook Email Attachments
  • [URL='www.slipstick.com/developer/save-attachments-to-the-hard-drive/']Save Attachments to the Hard Drive
  • [URL='www.slipstick.com/developer/code-samples/save-outlook-email-pdf/']Save Outlook Email as a PDF
  • [URL='www.slipstick.com/outlook/email/sort-messages-sender-domain/']Sort messages by Sender domain
  • [URL='www.slipstick.com/developer/talking-reminders/']Talking Reminders
  • [URL='www.slipstick.com/outlook/rules/create-a-rule-with-wildcards/']To create a rule with wildcards

www.slipstick.com/developer/process-messages-received-day-week/

Khóa học Power PI – Ứng dung trong Nhân sự
Khóa học SprinGO phù hợp

Khóa học Power PI – Ứng dung trong Nhân sự

TỔNG QUAN KHÓA HỌC: POWER BI CHO NGÀNH NHÂN SỰ Khóa học Power BI cho Nhân sự được thiết kế dành riêng cho các...

Xem khóa học
★★★★★ 5 ★ 1 👤 0 ▥ 0
Quảng cáo

Bạn nên đọc

Bình luận

Quảng cáo

Cũ vẫn chất

Xem thêm