Compartir a través de


Count the number of items in your entire mailbox

First, let me preface this entry by saying that I am an abysmal programmer. I use my basic programming skills to write code that helps me do things in the applications I use, and since these are usually one-time things, I don't worry too much about performance or error checking.

Phew, now that that's over with:

During the Outlook 2003 development cycle, we ran into a bug where it appeared that the number of items that Outlook had in the local cache was different than the number of items on the server. In order to quickly verify this, I wrote some code to iterate through the list of folders, count the number of items in each folder and its subfolders, and add that all up. The user then ran the code while in offline mode and while connected directly to the server with no cache, to see if the values matched.

If you have a need for this (or want to do any operation or check across all folders in the mailbox), here's how you get it to run in Outlook:

1. Start Outlook
2. Tools | Macros | Security
3. Choose “Medium”, which will prompt you on whether or not you want to run macros (VBA). You may need to restart Outlook at this point in order for that setting to take effect, I'm not sure.
4. Tools | Macros | Visual Basic Editor
5. Doubleclick on This Outlook Session on the left, which will open the code window on the right
6. In the code window, paste the code from the bottom of this entry (in courier font)
7. View | Immediate to make the immediate window show up
8. Put the cursor in the “CountItemsInMBX” procedure
9. Press F5


Sub CountItemsInMBX()

Dim outapp As Outlook.Application
Set outapp = CreateObject("Outlook.Application")
Dim olns As Outlook.NameSpace
Set olns = outapp.GetNamespace("MAPI")

Debug.Print GetSubFolderCount(olns.GetDefaultFolder(olFolderInbox).Parent)

End Sub

Function GetSubFolderCount(objParentFolder As MAPIFolder) As Long
Dim currentFolders As Folders
Dim fldCurrent As MAPIFolder

Set currentFolders = objParentFolder.Folders
If currentFolders.Count > 0 Then
Set fldCurrent = currentFolders.GetFirst
While Not fldCurrent Is Nothing
TempFolderCount = TempFolderCount + GetSubFolderCount(fldCurrent)
Set fldCurrent = currentFolders.GetNext
Wend
Debug.Print objParentFolder.Name & " - " & objParentFolder.Items.Count
GetSubFolderCount = TempFolderCount + objParentFolder.Items.Count
Else
Debug.Print objParentFolder.Name & " - " & objParentFolder.Items.Count
GetSubFolderCount = objParentFolder.Items.Count
End If

End Function


This will display a per-folder item count as well as a sum of all of those item counts, in the immediate window. A few line change could output the results to a file instead.

Note: While the code is running, you probably won't be able to do anything else in Outlook. If it goes haywire or takes too long, hit Ctrl+Break and then blame my cruddy code :-)

Some other resources for Outlook programming:

Comments

  • Anonymous
    January 05, 2004
    Thanks for that :-)
  • Anonymous
    July 15, 2004
    This connects to the Outlook client app-can we connect directly to the exchange server.