Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.