Count how many emails are in each colour category

hets

New Member
Joined
Aug 17, 2012
Messages
41
hi,
I need help with counting how many emails in outlook 2010 are under each category. Category are colours, therefore count how many emails are in red category, blue category, etc. Can this be done using vba? Please help!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
First, you'll need to set two references. In the Visual Basic Editor (Alt+F11), select Tools, and then Refences. Then select Microsoft Outlook Object Library and Microsoft Scripting Runtime, and click OK. Then place the following code in a standard module. The code will loop through each item (emails and meetings) within your inbox (you can change the folder, as desired). In the active sheet, the macro will list the categories and corresponding email count, starting at A2...

Code:
[FONT=Courier New][COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]
[COLOR=darkblue]Sub[/COLOR] test()
    [COLOR=darkblue]Dim[/COLOR] oDict [COLOR=darkblue]As[/COLOR] Scripting.Dictionary
    [COLOR=darkblue]Dim[/COLOR] olApp [COLOR=darkblue]As[/COLOR] Outlook.Application
    [COLOR=darkblue]Dim[/COLOR] olNS [COLOR=darkblue]As[/COLOR] Outlook.Namespace
    [COLOR=darkblue]Dim[/COLOR] olFolder [COLOR=darkblue]As[/COLOR] Outlook.MAPIFolder
    [COLOR=darkblue]Dim[/COLOR] olItem [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] arrData() [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] CategoryCnt [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] c [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
    
    [COLOR=darkblue]Set[/COLOR] oDict = [COLOR=darkblue]New[/COLOR] Scripting.Dictionary
    
    [COLOR=darkblue]Set[/COLOR] olApp = [COLOR=darkblue]New[/COLOR] Outlook.Application
    
    [COLOR=darkblue]Set[/COLOR] olNS = olApp.GetNamespace("MAPI")
    
    [COLOR=darkblue]Set[/COLOR] olFolder = olNS.GetDefaultFolder(olFolderInbox)
    
    CategoryCnt = olNS.Categories.Count
    
    [COLOR=darkblue]ReDim[/COLOR] arrData(1 [COLOR=darkblue]To[/COLOR] 2, 1 [COLOR=darkblue]To[/COLOR] CategoryCnt)
    
    c = 0
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] olItem [COLOR=darkblue]In[/COLOR] olFolder.Items
        [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] oDict.Exists(olItem.Categories) [COLOR=darkblue]Then[/COLOR]
            c = c + 1
            arrData(1, c) = olItem.Categories
            arrData(2, c) = 1
            oDict.Add olItem.Categories, c
        [COLOR=darkblue]Else[/COLOR]
            arrData(2, oDict.Item(olItem.Categories)) = arrData(2, oDict.Item(olItem.Categories)) + 1
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]Next[/COLOR] olItem
    
    [COLOR=darkblue]ReDim[/COLOR] [COLOR=darkblue]Preserve[/COLOR] arrData(1 To 2, 1 To c)
    
    Range("A2").Resize(UBound(arrData, 2), UBound(arrData, 1)).Value = Application.Transpose(arrData)
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
[/FONT]
 
Upvote 0
Thank you for your help. I tried the above code but got an error msg on Set oDict = New Scripting.Dictionary, Compile error: User-defined type not defined. How can I change that to make it work?
 
Upvote 0
I realised I was using the code in excel, instead of outlook. But now I get an error msg sayin : Compile error: Sub or function not defined for Range("A2").Resize(UBound(arrData, 2), UBound(arrData, 1)).Value = Application.Transpose(arrData). How can I correct that?
 
Upvote 0
The code that I offered was meant to run from Excel, and places the results in the active worksheet of an opened workbook. If you want to run the code from Outlook, you'll need to specify where you'd like to return the results. Otherwise, to run it from Excel, you'll need to make sure that you set a reference for two libraries, as follows...

Code:
Visual Basic Editor (Alt+F11) > Tools > References

Select Microsoft Outlook Object Library 

Select Microsoft Scripting Runtime

Click OK

Exit the Visual Basic Editor (Alt+Q)

And, you'll need to place the code in a standard module...

Code:
Press Alt+F11

Select Insert > Module

Copy/paste the code in the module

Press Alt+Q
 
Upvote 0

Forum statistics

Threads
1,223,941
Messages
6,175,537
Members
452,652
Latest member
eduedu

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