We help IT Professionals succeed at work.

MS Outlook - Count emails

BSTIT
BSTIT asked
on
Hi Experts

Is there a way of counting the number of emails a specific mailbox in our cokpnay receives daily. Is there a way to get some sort of report or some way of running a command that adds the number of emais. Maybe even getting this report or count emailed to another user daily.
Comment
Watch Question

Top Expert 2010

Commented:
Hi, BSTIT.

I can do it with a simple bit of scripting at the client.  Outlook would have to be open for the count to take place and the report to be sent.  If that's an acceptable solution, then I can post the code and instructions.

Author

Commented:
hi, this sounds perfect, i just need to have a coutn of incoming emails for now. Is it a simple batch file *.bat file that i can run as i am not good with scripts
Top Expert 2010

Commented:
No, not a batch file.  The code would have to be added to the Outlook client of the mailbox you want to get the count for.  It'd count messages as they arrived.  A second script would query the client to get that count.  

Author

Commented:
ok, will you show me step by step how to add it and get it to do what i want? please, thanks
Top Expert 2010
Commented:
Here's the code for counting up the items.  Follow these instructions to add it to Outlook.

Outlook 2003

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
4.  Copy the code from the Code Snippet box and paste it into the right-hand pane of
5.  Outlook's VB Editor window
6.  Edit the code as needed.  I included comment lines wherever something needs to or can change
7.  Click the diskette icon on the toolbar to save the changes
8.  Close the VB Editor
9.  Click Tools > Macro > Security
10. Set the Security Level to Medium
11. Close Outlook
12. Start Outlook
13. Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run.  Say yes.

Outlook 2007

1.  Start Outlook
2.  Click Tools > Macro > Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
4.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
5.  Edit the code as needed.  I included comment lines wherever something needs to or can change
6.  Click the diskette icon on the toolbar to save the changes
7.  Close the VB Editor
8.  Click Tools > Trust Center
9.  Click Macro Security
10. Set Macro Security to "Warnings for all macros"
11. Click OK
12. Close Outlook
13. Start Outlook.  Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run.  Say yes.

Once you have the code in place and we know it's counting the items, then I'll post the code for retrieving the count.
Dim WithEvents olkInbox As Outlook.Items

Private Sub Application_Quit()
    Set olkInbox = Nothing
End Sub

Private Sub Application_Startup()
    Set olkInbox = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub olkInbox_ItemAdd(ByVal Item As Object)
    If Item.Class = olMail Then
        Add2Count
    End If
End Sub

Sub Add2Count()
    'Edit the path on the next line'
    Const FILE_PATH = "c:\Users\David\Documents\"
    Dim objFSO As Object, objFile As Object, lngCounter As Long, strDate As String
    lngCounter = 0
    strDate = Format(Date, "yyyy-mm-dd")
    Set objFSO = CreateObject("Scripting.FileSystemobject")
    If objFSO.FileExists(FILE_PATH & strDate & ".txt") Then
        Set objFile = objFSO.OpenTextFile(FILE_PATH & strDate & ".txt", ForReading)
        lngCounter = objFile.ReadAll
    Else
        Set objFile = objFSO.CreateTextFile(FILE_PATH & strDate & ".txt")
    End If
    objFile.Close
    lngCounter = lngCounter + 1
    Set objFile = objFSO.CreateTextFile(FILE_PATH & strDate & ".txt")
    objFile.Write lngCounter
    objFile.Close
    Set objFile = Nothing
    Set objFSO = Nothing
End Sub

Open in new window

Author

Commented:
HI, i basically need to know the total number of received emails from a specific user into the mailbox. Will the code above work. For example: if the user receives 1000 emails per day but from a specific sender they onyl receive 200 emails then i want a report that will say the user received 200 emails from this specific sender
Top Expert 2010

Commented:
Your question made no mention of emails from a specific sender, so I didn't write the code to check who the message is from.  The question only asked to count the number of messages received in a mailbox.  The code does do that.  I can modify the code to check who it's from, but that will cause problems if you are using Outlook 2003 or earlier.

Author

Commented:
i see, sorry about this, please can you do it for outlook 2007 thanks
Top Expert 2010

Commented:
Ok.  Replace the code with the version below.  Pay attention to the comments in the code.
Dim WithEvents olkInbox As Outlook.Items

Private Sub Application_Quit()
    Set olkInbox = Nothing
End Sub

Private Sub Application_Startup()
    Set olkInbox = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub olkInbox_ItemAdd(ByVal Item As Object)
    If Item.Class = olMail Then
        'Change the name on the next line.  It must EXACTLY match the name you want to trigger on.'
        If Item.SenderName = "John Doe" Then
            Add2Count
        End If
    End If
End Sub

Sub Add2Count()
    'Edit the path on the next line'
    Const FILE_PATH = "c:\Users\David\Documents\"
    Dim objFSO As Object, objFile As Object, lngCounter As Long, strDate As String
    lngCounter = 0
    strDate = Format(Date, "yyyy-mm-dd")
    Set objFSO = CreateObject("Scripting.FileSystemobject")
    If objFSO.FileExists(FILE_PATH & strDate & ".txt") Then
        Set objFile = objFSO.OpenTextFile(FILE_PATH & strDate & ".txt", ForReading)
        lngCounter = objFile.ReadAll
    Else
        Set objFile = objFSO.CreateTextFile(FILE_PATH & strDate & ".txt")
    End If
    objFile.Close
    lngCounter = lngCounter + 1
    Set objFile = objFSO.CreateTextFile(FILE_PATH & strDate & ".txt")
    objFile.Write lngCounter
    objFile.Close
    Set objFile = Nothing
    Set objFSO = Nothing
End Sub

Open in new window