Sub CountDatedEmails()
'Declare Outlook app and folder object variables.
Dim objOutlook As Object, objnSpace As Object
Dim objFolder As Object, objFolderA As Object, objFolderB As Object
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")
'Verify existence of MIS folder as direct subfolder of Personal Folders.
On Error Resume Next
Set objFolder = objnSpace.Folders("Personal Folders").Folders("MIS")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder named MIS.", 48, "Cannot continue"
Exit Sub
End If
'Verify existence of Enquiries folder as direct subfolder #1 of Personal Folders.
On Error Resume Next
Set objFolderA = objnSpace.Folders("Personal Folders").Folders("MIS").Folders("Enquiries")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder named Enquiries exists in the MIS folder.", 48, "Cannot continue"
Exit Sub
End If
'Verify existence of Enquiries folder as direct subfolder #2 of Personal Folders.
On Error Resume Next
Set objFolderB = objnSpace.Folders("Personal Folders").Folders("MIS").Folders("Application")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder named Application exists in the MIS folder.", 48, "Cannot continue"
Exit Sub
End If
'All folders are present, OK to continue.
'Declare and define the myDate variable to be yesterday's date.
Dim myDate As Date
myDate = DateSerial(Year(Date), Month(Date), Day(Date) - 1)
'Declare and define the count and date variables for all 3 folders.
Dim iCount As Integer
Dim EmailCountMIS As Integer, EmailCountEnquiries As Integer, EmailCountApplication As Integer
Dim DateCountMIS As Integer, DateCountEnquiries As Integer, DateCountApplication As Integer
'Count total and yesterday's received emails in the MIS folder:
EmailCountMIS = objFolder.Items.Count: DateCountMIS = 0
For iCount = 1 To EmailCountMIS
With objFolder.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCountMIS = DateCountMIS + 1
End With
Next iCount
'Count total and yesterday's received emails in the MIS > Enquiries folder:
EmailCountEnquiries = objFolderA.Items.Count: DateCountEnquiries = 0
For iCount = 1 To EmailCountEnquiries
With objFolderA.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCountEnquiries = DateCountEnquiries + 1
End With
Next iCount
'Count total and yesterday's received emails in the MIS > Application folder:
EmailCountApplication = objFolderB.Items.Count: DateCountApplication = 0
For iCount = 1 To EmailCountApplication
With objFolderB.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCountApplication = DateCountApplication + 1
End With
Next iCount
'Advise the user of all counts for the three folders.
MsgBox _
"MIS folder email count - -" & vbCrLf & _
"Total: " & vbTab & vbTab & EmailCountMIS & vbCrLf & _
"Yesterday: " & vbTab & DateCountMIS & vbCrLf & vbCrLf & _
"MIS > Enquiries folder email count - -" & vbCrLf & _
"Total: " & vbTab & vbTab & EmailCountEnquiries & vbCrLf & _
"Yesterday: " & vbTab & DateCountEnquiries & vbCrLf & vbCrLf & _
"MIS > Application folder email count - -" & vbCrLf & _
"Total: " & vbTab & vbTab & EmailCountApplication & vbCrLf & _
"Yesterday: " & vbTab & DateCountApplication, , "Email counts:"
'Release object variable memory
Set objFolder = Nothing
Set objFolderA = Nothing
Set objFolderB = Nothing
Set objnSpace = Nothing
Set objOutlook = Nothing
End Sub