1

I have edited VBA which I found in Stack Overflow to suit my needs.

It gives me the count of emails of my default inbox date wise and count of unread emails without dates.

I need it to count the emails of my shared mailbox. For example Redstreamattmail and not the default mailbox, DATE WISE for overall emails and unread emails.

Sub HowManyEmails()    

Dim objOutlook As Object, objnSpace As Object, objFolder As MAPIFolder    
Dim EmailCount As Integer    

Dim a As Outlook.Application    
Dim b As Outlook.NameSpace    
Dim c As Outlook.MAPIFolder    

Set a = New Outlook.Application    
Set b = a.GetNamespace("MAPI")    
Set c = b.GetDefaultFolder(olFolderInbox)    
d = c.UnReadItemCount    

Set objOutlook = CreateObject("Outlook.Application")    
Set objnSpace = objOutlook.GetNamespace("MAPI")    

On Error Resume Next    

Set objFolder = objnSpace.GetDefaultFolder(olFolderInbox)    

If Err.Number <> 0 Then    
    Err.Clear    
    MsgBox "No such folder."    
    Exit Sub    
End If    

EmailCount = objFolder.Items.Count    

MsgBox "Number of emails in the folder: " & EmailCount & " Total Unread     email count are   " & d    

Dim dateStr As String    
Dim myItems As Outlook.Items    
Dim dict As Object    
Dim msg, msg1 As String    

Set dict = CreateObject("Scripting.Dictionary")    
Set myItems = objFolder.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    

' Output counts per day:    
For Each o In dict.Keys    
    msg = msg & o & ":    " & dict(o) & " Email items" & vbCrLf    
Next    
msg1 = "unread Emails are        " & d    

Set objFolder = Nothing    
Set objnSpace = Nothing    
Set objOutlook = Nothing    

'Send Email    
Set OutApp = CreateObject("outlook.Application")    
Set OutMail = OutApp.CreateItem(o)    

With OutMail    
    .Subject = "Count of emails"    
    .To = "name@company.com;"    
    .Body = msg & msg1    
    .Display    
    '.Send    
End With     

Set OutMail = Nothing    
Set OutApp = Nothing    

End Sub    
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Shaik
  • 11
  • 3
  • Possible duplicate of [Get reference to additional Inbox](http://stackoverflow.com/questions/9076634/get-reference-to-additional-inbox) – niton Apr 13 '16 at 20:57
  • Did you try debugging? set breakpoints, watch variables ... try add method on dict ... – Adam Silenko Apr 13 '16 at 20:58

1 Answers1

1

Try:

Set c =  b.Folders("Name of shared mailbox")

Where you put the correct folder name in there.

OpiesDad
  • 3,385
  • 2
  • 16
  • 31
  • It worked for me thanks so much, but i noticed i am not getting correct count of emails each day in the mailbox for all emails, however unread email count i get it right. any edition i need to do to get the correct email count? – Shaik Apr 14 '16 at 17:42
  • You can ask a new question so it is noticed. Include the code for GetDate. Not a duplicate of http://stackoverflow.com/questions/15796392/counting-emails-in-outlook-2010-in-specific-folder as there is no accepted answer to the incorrect count. – niton Apr 14 '16 at 20:34
  • Can we do something so that email is prompted with only unread emails for each day?/ – Striker Sep 22 '18 at 13:40
  • @Striker See advice posted in the second comment on this answer. If you have a new question, please ask a new question so that it can be addressed. [If I knew the answer off the top of my head, I'd probably tell you, but I answered this over 2 years ago and don't generally work with this anymore] – OpiesDad Sep 25 '18 at 16:55