{"id":68173,"date":"2006-01-16T16:58:00","date_gmt":"2006-01-16T16:58:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/16\/how-can-i-get-total-size-and-number-of-items-in-an-office-outlook-folder\/"},"modified":"2006-01-16T16:58:00","modified_gmt":"2006-01-16T16:58:00","slug":"how-can-i-get-total-size-and-number-of-items-in-an-office-outlook-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-total-size-and-number-of-items-in-an-office-outlook-folder\/","title":{"rendered":"How Can I Get Total Size and Number of Items in an Office Outlook Folder?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! How can I get the total number of items &#8211; and the total size &#8211; of all the items in my Inbox and Sent Items folders?<BR><BR>&#8212; CS<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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).<\/P>\n<P>Of course, it\u2019s a good rule of thumb never to pattern your life after any of the Scripting Guys. With that in mind, here\u2019s another way to determine the number of items (and total size) in both your Inbox and Sent Items folders:<\/P><PRE class=\"codeSample\">Const olFolderInbox = 6\nConst olFolderSentMail = 5<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)<\/p>\n<p>Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)\nSet colItems = objFolder.Items\nWscript.Echo &#8220;No. of items in Inbox: &#8221; &amp; colItems.Count<\/p>\n<p>For Each objItem in colItems\n    intSize = intSize + objItem.Size\nNext<\/p>\n<p>Wscript.Echo &#8220;Size of Inbox: &#8221; &amp; Int(intSize \/ 1024) &amp; &#8221; KB&#8221;<\/p>\n<p>intSize = 0<\/p>\n<p>Set objFolder = objNamespace.GetDefaultFolder(olFolderSentMail)\nSet colItems = objFolder.Items\nWscript.Echo &#8220;No. of items in Sent Mail folder: &#8221; &amp; colItems.Count<\/p>\n<p>For Each objItem in colItems\n    intSize = intSize + objItem.Size\nNext<\/p>\n<p>Wscript.Echo &#8220;Size of Sent Mail folder: &#8221; &amp; Int(intSize \/ 1024) &amp; &#8221; KB&#8221;\n<\/PRE>\n<P>Yes, we know: it <I>does<\/I> look a little complicated, doesn\u2019t it? But don\u2019t worry; we\u2019ll walk you through the code and explain everything to you. After all, that\u2019s what we\u2019re here for.<\/P>\n<P>Well, that and to clean up the offices of the <I>real<\/I> Microsoft employees after they leave for the day. But you know what we mean.<\/P>\n<P>To begin with, we define a pair of constants, which we\u2019ll use later to indicate which folders we want to look at:<\/P><PRE class=\"codeSample\">Const olFolderInbox = 6\nConst olFolderSentMail = 5\n<\/PRE>\n<P>We then use these two lines of code to create an instance of the <B>Outlook.Application<\/B> object and to bind to the MAPI namespace (the only namespace you <I>can<\/I> bind to):<\/P><PRE class=\"codeSample\">Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\n<\/PRE>\n<P>At this point we should mention that our script assumes that Outlook is already running. If it\u2019s not, you\u2019ll have to make a minor modification to the script in order to start Outlook and to log on. We won\u2019t go into that in today\u2019s column; that\u2019s because we have an <I>Office Space<\/I> column lying around somewhere that explains how to start Outlook from scratch.<\/P>\n<P>Oh: turns out that column is right <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/jun05\/tips0614.mspx\"><B>here<\/B><\/A>.<\/P>\n<P>After connecting to Outlook we next need to bind to the Inbox and determine the number of items found there. That\u2019s easy; in fact, it takes just three lines of code:<\/P><PRE class=\"codeSample\">Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)\nSet colItems = objFolder.Items\nWscript.Echo &#8220;No. of items in Inbox: &#8221; &amp; colItems.Count\n<\/PRE>\n<P>As you can see, we begin by calling the <B>GetDefaultFolder<\/B> 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 <B>Count<\/B> 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\u2019s exactly what we do in line 3.<\/P>\n<P>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\u2019s because there is no Size property that tells us the size of a folder; instead, we have to calculate the size ourselves. <\/P>\n<P>Hey, relax: it\u2019s not that big of a deal. Each item in the Inbox (e.g., each mail message) <I>does<\/I> have a <B>Size<\/B> 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:<\/P><PRE class=\"codeSample\">For Each objItem in colItems\n    intSize = intSize + objItem.Size\nNext\n<\/PRE>\n<P>After we\u2019ve walked through the entire collection we\u2019ll 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\u2019s what we do here:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Size of Inbox: &#8221; &amp; Int(intSize \/ 1024) &amp; &#8221; KB&#8221;\n<\/PRE>\n<P>Granted, this construction looks a little weird: <B>Int(intSize \/ 1024)<\/B>. But all we\u2019re doing is taking the size of the Inbox in bytes (a value stored in the variable intSize) and dividing it by 1024. We\u2019re then taking <I>that<\/I> value (the size of the Inbox in kilobytes) and using the <B>Int<\/B> 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.<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">Set objFolder = objNamespace.GetDefaultFolder(olFolderSentMail)\n<\/PRE>\n<P>Etc. etc. etc.<\/P>\n<P>Granted, a script like this one lacks the personal touch of a threatening email from the Exchange admin. On the other hand, it <I>does<\/I> give you slightly more precise information than \u201ctoo big\u201d and \u201ctoo many.\u201d We\u2019ll leave it up to you to decide which method will work best for you.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get the total number of items &#8211; and the total size &#8211; of all the items in my Inbox and Sent Items folders?&#8212; CS 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[212,49,3,5],"class_list":["post-68173","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-outlook","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I get the total number of items &#8211; and the total size &#8211; of all the items in my Inbox and Sent Items folders?&#8212; CS 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68173"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68173\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=68173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}