How to Quickly Get the Total Count of Items in a Folder and All Its Subfolders via Outlook VBA

If you have multiple subfolders under a certain folder and now you want to count the items in this folder and all its subfolders, you can use the way introduced in this article.

Quickly Get the Total Count of Items in a Folder and All Its Subfolders via Outlook VBAIn general, to check the total count of items in one folder, you have two ways. One is to select this folder and then you can view the count of items in the lower-left corner of status bar. The other one is to change folder properties to set it to show total number of items. However, if there are several subfolders under this folder, both the two means above are incapable of including the subfolders in counting. Therefore, if you would like to get the total count of items in a folder and all its subfolders, you need to seek other means, such as the following one. It is utilizing VBA code to quickly achieve such a total count. Please read on to get its elaborate steps and VBA codes.

Get the Total Count of Items in a Folder and All Its Subfolders

  1. At the very outset, you can start your Outlook program as usual.
  2. Then press “Alt + F11” key buttons to show Outlook VBA editor.
  3. Next you can open an empty module.
  4. Subsequently, copy and paste the following VBA codes into this module.
Sub CountItems()
    Dim objMainFolder As Outlook.Folder
    Dim lItemsCount As Long
 
    'Select a folder
    Set objMainFolder = Outlook.Application.Session.PickFolder
 
    If objMainFolder Is Nothing Then
       MsgBox "You choose select a valid folder!", vbExclamation + vbOKOnly, "Warning for Pick Folder"
    Else
       'Initialize the total count
       lItemsCount = 0
       Call LoopFolders(objMainFolder, lItemsCount)
    End If
 
    'Display a message for the total count
    MsgBox "There are " & lItemsCount & " items in the " & objMainFolder.Name & " folder Including its subfolders.", vbInformation, "Count Items"
End Sub

Sub LoopFolders(ByVal objCurrentFolder As Outlook.Folder, lCurrentItemsCount As Long)
    Dim objSubfolder As Outlook.Folder
 
    lCurrentItemsCount = lCurrentItemsCount + objCurrentFolder.Items.Count
 
    'Process all folders and subfolders recursively
    If objCurrentFolder.Folders.Count Then
       For Each objSubfolder In objCurrentFolder.Folders
           Call LoopFolders(objSubfolder, lCurrentItemsCount)
       Next
    End If
End Sub

VBA Code - Get the Total Count of Items in a Folder and All Its Subfolders

  1. After that, you should change your Outlook macro security level to low.
  2. Finally you can have a try.
  • In the new macro window, press F5 key button.
  • At once, you will be required to select a folder.Select a Folder
  • Immediately, you will receive a new message prompting the total count of the items in the selected folder and all its subfolders, like the picture below:Count Items

Archive Old Items in Time

It is always suggested to archive the old items in your PST file as soon as possible. Otherwise, with more and more items accumulating in this file, it will be prone to errors and corruptions. Once your PST file gets compromised, you will spend lots of efforts getting back the corrupt Outlook data. It’ll be pretty difficult unless you recur to an experienced tool like DataNumen Outlook Repair.

Author Introduction:

Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including corrupt SQL Server and outlook repair software products. For more information visit www.datanumen.com

Leave a Reply

Your email address will not be published. Required fields are marked *