Count Emails from a specified Email Address Outlook VBA

RosieAU

New Member
Joined
Jun 26, 2015
Messages
3
Hi guys,

I am trying to create a Macro in Outlook for my daily call centre reporting. There is an inbox and then a folder where each agent keeps their completed emails. The figures that I report are as follows:
Emails received (by date)
Emails Open
Date of oldest open email

I've managed to find what I need so far but I have one problem that I can't find any information for. The agents who complete the emails CC the group mailbox when replying to customer emails. I would like to exclude these emails from my count either by excluding emails where the specified email address is in the CC field, or by excluding emails where the sender is a specified email address. Here is what I have so far:

Sub HowManyTASEmails()


Dim objOutlook As Object, objnSpace As Object, objFolder As MAPIFolder
Dim EmailCount As Integer
Set objOutlook = CreateObject("Outlook.Application")
Set objnSpace = objOutlook.GetNamespace("MAPI")


On Error Resume Next
Set objFolder = objnSpace.Folders("TAS").Folders("Inbox")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder Inbox."
Exit Sub
End If

On Error Resume Next
Set objFolderA = objnSpace.Folders("TAS").Folders("Inbox").Folders("Emails - XXXX Completed")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder Emails - XXXX Completed."
Exit Sub
End If

On Error Resume Next
Set objFolderB = objnSpace.Folders("TAS").Folders("Inbox").Folders("Emails - YYYY Completed")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder Emails - YYYY Completed."
Exit Sub
End If

On Error Resume Next
Set objFolderC = objnSpace.Folders("TAS").Folders("Inbox").Folders("Emails - ZZZZ Completed")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder Emails - ZZZZ Completed."
Exit Sub
End If

On Error Resume Next
Set objFolderD = objnSpace.Folders("TAS").Folders("Inbox").Folders("Emails - AAAA Completed")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder Emails - AAAA Completed."
Exit Sub
End If

On Error Resume Next
Set objFolderE = objnSpace.Folders("TAS").Folders("Inbox").Folders("XXXX Agent Emails")
If Err.Number <> 0 Then
Err.Clear
MsgBox "No such folder Emails - XXXX Agent Emails."
Exit Sub
End If

EmailCount = objFolder.Items.Count


MsgBox "Number of emails in the folder: " & EmailCount, , "email count"


objFolder.Items.Sort "[ReceivedTime]", False
Set oldestMessage = objFolder.Items.GetLast
MsgBox "Date of Oldest Open Email: " & oldestMessage.ReceivedTime

Dim dateStr As String
Dim myItems As Outlook.Items
Dim myItemsA As Outlook.Items
Dim myItemsB As Outlook.Items
Dim myItemsC As Outlook.Items
Dim myItemsD As Outlook.Items
Dim myItemsE As Outlook.Items
Dim dict As Object
Dim msg As String
Set dict = CreateObject("Scripting.Dictionary")
Set myItems = objFolder.Items
Set myItemsA = objFolderA.Items
Set myItemsB = objFolderB.Items
Set myItemsC = objFolderC.Items
Set myItemsD = objFolderD.Items
Set myItemsE = objFolderE.Items
myItems.SetColumns ("SentOn")
' Determine date of each message:
For Each myItem In myItems
dateStr = GetDate(myItem.SentOn)
If Not dict.Exists(dateStr) Then
dict(dateStr) = 0
End If
dict(dateStr) = CLng(dict(dateStr)) + 1
Next myItem
myItemsA.SetColumns ("SentOn")
' Determine date of each message:
For Each myItem In myItemsA
dateStr = GetDate(myItem.SentOn)
If Not dict.Exists(dateStr) Then
dict(dateStr) = 0
End If
dict(dateStr) = CLng(dict(dateStr)) + 1
Next myItem
myItemsB.SetColumns ("SentOn")
' Determine date of each message:
For Each myItem In myItemsB
dateStr = GetDate(myItem.SentOn)
If Not dict.Exists(dateStr) Then
dict(dateStr) = 0
End If
dict(dateStr) = CLng(dict(dateStr)) + 1
Next myItem
myItemsC.SetColumns ("SentOn")
' Determine date of each message:
For Each myItem In myItemsC
dateStr = GetDate(myItem.SentOn)
If Not dict.Exists(dateStr) Then
dict(dateStr) = 0
End If
dict(dateStr) = CLng(dict(dateStr)) + 1
Next myItem
myItemsD.SetColumns ("SentOn")
' Determine date of each message:
For Each myItem In myItemsD
dateStr = GetDate(myItem.SentOn)
If Not dict.Exists(dateStr) Then
dict(dateStr) = 0
End If
dict(dateStr) = CLng(dict(dateStr)) + 1
Next myItem
myItemsE.SetColumns ("SentOn")
' Determine date of each message:
For Each myItem In myItemsE
dateStr = GetDate(myItem.SentOn)
If Not dict.Exists(dateStr) Then
dict(dateStr) = 0
End If
dict(dateStr) = CLng(dict(dateStr)) + 1
Next myItem


' Output counts per day:
msg = ""
For Each o In dict.Keys
msg = msg & o & ": " & dict(o) & " items" & vbCrLf
Next
MsgBox msg


Set objFolder = Nothing
Set objFolderA = Nothing
Set objFolderB = Nothing
Set objFolderC = Nothing
Set objFolderD = Nothing
Set objFolderE = Nothing
Set objnSpace = Nothing
Set objOutlook = Nothing
End Sub


Function GetDate(dt As Date) As String
GetDate = Year(dt) & "-" & Month(dt) & "-" & Day(dt)
End Function


If possible it would be really awesome if the macro could also exclude today's emails but this is not as important as excluding the CC emails.

Thanks!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try using the Restrict method of the Items collection, which returns a new collection containing all the items from the original that match the filter. So, for example, to return a filtered list of items that excludes a specific email address...

Code:
Set myItems= objFolder.Items.Restrict("[SenderEmailAddress] <> 'john@example.com'")

For a count of filtered items...

Code:
EmailCount = myItems.Count

Hope this helps!
 
Upvote 0
Thanks Domenic! For some reason it didn't work with email addresses formatted as john.smith@example.com - I changed it to:
Set myItems = objFolder.Items.Restrict("[From] <> 'Smith, John' ")
And it works great! :)
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top