Is your Outlook mailbox overflowing? TreeMap it!

In this post I published 100 lines of code that showed how to get a visual representation of your hard disk to see which folders take the most space. It showed that my Outlook mailbox was half a gigabyte. (“C:\DOCUMENTS AND SETTINGS\CALVINH\LOCAL SETTINGS\APPLICATION DATA\MICROSOFT\OUTLOOK” ). I am using some great features of Outlook: Cached Exchange Mode which makes my entire mailbox available offline and RPC over HTTP which allows me to use Outlook over an internet connection without logging onto the network at work. I just knew cached mode used a lot of space!

Well it was really simple to add another method onto the class to map my Outlook Mailbox visually to see what was taking the most space within that half gigabyte. The rest of the code just works as expected: Tooltips show the folder name and size, and you can click on a rectangle to drill down into the tree.

As an exercise, you could even modify the code so that if you navigate through your hard disk folders to the Outlook folder on your hard disk, it seamlessly just drills down into your mailbox inside.

Use this line to create the form. Here, I’m using “*” to represent an Outlook 2003 mailbox. Be sure to change the mailbox name. Note it uses GETOBJECT function which assumes Outlook is already running.

oForm=CREATEOBJECT("TreeMapForm","*",.f.)

In the INIT method, Replace the this.DoDir(cPath) line with

                  IF cPath="*"

            loApp = GETOBJECT("","Outlook.application")

                        oSpace=loApp.GetNameSpace("MAPI")

                        oFolder=oSpace.Folders("MailBox - Calvin Hsia")

                        this.DoOutlook(cPath,1,oFolder)

                  ELSE

                        this.DoDir(cPath)

                  ENDIF

And then add this method

      PROCEDURE DoOutlook(cPath as String, nDepth as Integer, oFolder as Outlook.MAPIFolder) as Number && Recursive routine to get folders and their sizes

            LOCAL oSubfolder as Outlook.MAPIFolder,oItem as Outlook.MailItem

            LOCAL nTotal, nFileTotal

            nFileTotal=0

            nTotal=0

            ?cPath,oFolder.Items.Count

            FOR EACH oSubfolder as Outlook.MAPIFolder IN oFolder.Folders

                  nTotal=nTotal+this.DoOutlook(cPath+"\"+oSubFolder.Name,nDepth+1,oSubFolder)

            ENDFOR

            FOR EACH oItem as Outlook.MailItem IN oFolder.Items

                  TRY

                        nFileTotal=nFileTotal+oItem.Size

                  CATCH

                  ENDTRY

            ENDFOR

            nTotal= nTotal+nFileTotal

            INSERT INTO dirs (Path,Depth,size) VALUES (cPath+'\',nDepth,nTotal) && insert the total subfolder info

            IF nFileTotal>0

                  INSERT INTO dirs (Path,Depth,size) VALUES (cPath+"*\",nDepth+1,nFileTotal) && for items within current folder

            ENDIF

      RETURN nTotal