How Can I Get Total Size and Number of Items in an Office Outlook Folder?

How Can I Get Total Size and Number of Items in an Office Outlook Folder?

  • Comments 2
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I get the total number of items - and the total size - of all the items in my Inbox and Sent Items folders?

-- CS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CS. You know, one of the Scripting Guys has a foolproof method for determining this information. What does he do? Nothing; instead, he just sits around and waits for the inevitable email from the Exchange admin telling him that his mailbox has once again exceeded its size limit and that he will no longer be able to send or receive mail until he gets rid of some stuff. At that point he knows the total size of the Inbox (too big) as well as the total number of items (too many).

Of course, it’s a good rule of thumb never to pattern your life after any of the Scripting Guys. With that in mind, here’s another way to determine the number of items (and total size) in both your Inbox and Sent Items folders:

Const olFolderInbox = 6
Const olFolderSentMail = 5

Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
   
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Set colItems = objFolder.Items
Wscript.Echo "No. of items in Inbox: " & colItems.Count

For Each objItem in colItems
    intSize = intSize + objItem.Size
Next

Wscript.Echo "Size of Inbox: " & Int(intSize / 1024) & " KB"

intSize = 0

Set objFolder = objNamespace.GetDefaultFolder(olFolderSentMail)
Set colItems = objFolder.Items
Wscript.Echo "No. of items in Sent Mail folder: " & colItems.Count

For Each objItem in colItems
    intSize = intSize + objItem.Size
Next

Wscript.Echo "Size of Sent Mail folder: " & Int(intSize / 1024) & " KB"

Yes, we know: it does look a little complicated, doesn’t it? But don’t worry; we��ll walk you through the code and explain everything to you. After all, that’s what we’re here for.

Well, that and to clean up the offices of the real Microsoft employees after they leave for the day. But you know what we mean.

To begin with, we define a pair of constants, which we’ll use later to indicate which folders we want to look at:

Const olFolderInbox = 6
Const olFolderSentMail = 5

We then use these two lines of code to create an instance of the Outlook.Application object and to bind to the MAPI namespace (the only namespace you can bind to):

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

At this point we should mention that our script assumes that Outlook is already running. If it’s not, you’ll have to make a minor modification to the script in order to start Outlook and to log on. We won’t go into that in today’s column; that’s because we have an Office Space column lying around somewhere that explains how to start Outlook from scratch.

Oh: turns out that column is right here.

After connecting to Outlook we next need to bind to the Inbox and determine the number of items found there. That’s easy; in fact, it takes just three lines of code:

Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
Set colItems = objFolder.Items
Wscript.Echo "No. of items in Inbox: " & colItems.Count

As you can see, we begin by calling the GetDefaultFolder method, passing along our constant olFolderInbox; this binds us to the Inbox folder. We then use the second line of code to return a collection of all the items found in the Inbox. As it turns out, Outlook collections all include a Count property, which tells us the number of items in the collection: in order to know the number of items in the Inbox all we have to do is echo the value of the Count property. And that’s exactly what we do in line 3.

Needless to say, retrieving the number of items in a folder is easy; determining the total size of the folder is a tiny bit more complicated. That’s because there is no Size property that tells us the size of a folder; instead, we have to calculate the size ourselves.

Hey, relax: it’s not that big of a deal. Each item in the Inbox (e.g., each mail message) does have a Size property, a property that expresses the size of the item in bytes. To determine the total size of the Inbox folder all we have to do is add up the sizes of the individual items in the folder. We can do that by setting up a For Each loop to walk through the collection, keeping a running total of the folder size as we go (a tally stored in a variable we named intSize). You know, something that looks like this:

For Each objItem in colItems
    intSize = intSize + objItem.Size
Next

After we’ve walked through the entire collection we’ll know the size of the Inbox folder. Although we could echo back that value in bytes, we decided to get a little fancy and divide the value by 1024; that gives us the size in kilobytes (KBs). That’s what we do here:

Wscript.Echo "Size of Inbox: " & Int(intSize / 1024) & " KB"

Granted, this construction looks a little weird: Int(intSize / 1024). But all we’re doing is taking the size of the Inbox in bytes (a value stored in the variable intSize) and dividing it by 1024. We’re then taking that value (the size of the Inbox in kilobytes) and using the Int function to drop the decimal point. That way we get back an Inbox size of, say, 11,318 KB rather than 11,318.462811 KB.

All we have to do now is set the value of intSize back to 0 and then start the process all over again, this time by binding to the Sent Items folder:

Set objFolder = objNamespace.GetDefaultFolder(olFolderSentMail)

Etc. etc. etc.

Granted, a script like this one lacks the personal touch of a threatening email from the Exchange admin. On the other hand, it does give you slightly more precise information than “too big” and “too many.” We’ll leave it up to you to decide which method will work best for you.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • can you use this script to determine the size of all 5 root folders in Outlook i.e. inbox; sentitems; deleteditems; contacts; calendars??

  • Nice work. Can you use this to determine size of entire mailbox? What I'd really like is to have an active display in my ribbon that shows the mailbox size live, as messages are received and deleted. I don't want to have to right click on the mailbox, select data file properties, folder size, and then wait for it to calculate. Thanks.