Ask Your Question
2

How can ReceivedTime in Outlook VBA be utilized to track the number of emails received yesterday?

asked 2023-05-09 08:31:04 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-09 08:42:02 +0000

david gravatar image

Here is an example of how you can use the ReceivedTime property in Outlook VBA to track the number of emails received yesterday:

Sub CountEmailsReceivedYesterday()
    Dim inbox As Outlook.Folder
    Dim items As Outlook.Items
    Dim yesterdayDate As Date
    Dim count As Integer
    Dim email As MailItem

    yesterdayDate = DateAdd("d", -1, Now())
    count = 0

    ' Set the Inbox folder and retrieve all emails
    Set inbox = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)
    Set items = inbox.Items

    ' Loop through all emails in the Inbox folder
    For Each email In items

        ' Check if the email was received yesterday
        If email.ReceivedTime >= yesterdayDate And email.ReceivedTime < Now() Then
            count = count + 1
        End If

    Next

    ' Display the total count of emails received yesterday in a message box
    MsgBox "Total Emails Received Yesterday: " & count

End Sub

In this code, we first set the date for yesterday using the DateAdd function. We then retrieve the default Inbox folder and all its items using the GetDefaultFolder method. We then loop through all the items in the folder, and for each email, we check if its ReceivedTime property is within yesterday's date. If so, we increment the count variable. Finally, we display the total count of emails received yesterday in a message box.

Note that this code only checks the Inbox folder. If you want to check other folders or change the time range for counting emails, you will need to modify the code accordingly.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-09 08:31:04 +0000

Seen: 8 times

Last updated: May 09 '23