PDA

View Full Version : VBA for count of emails in a folder and subfolder



jrisebo
01-16-2020, 04:05 PM
I want to count how many emails are in a folder (including sub folders) and outlook is not providing it. If I have a folder called '2020' whats some good code to tell me how many emails total are in that folder and subfolders?
Thanks

gmayor
01-17-2020, 03:51 AM
You will need a macro


Sub CountContents()
Dim cFolders As Collection
Dim olFolder As Outlook.Folder
Dim SubFolder As Folder
Dim olNS As Outlook.NameSpace
Dim lngCount As Long: lngCount = 0
Set cFolders = New Collection
Set olNS = GetNamespace("MAPI")
cFolders.Add olNS.PickFolder
Do While cFolders.Count > 0
Set olFolder = cFolders(1)
cFolders.Remove 1
lngCount = lngCount + olFolder.items.Count
For Each SubFolder In olFolder.folders
cFolders.Add SubFolder
Next SubFolder
Loop
MsgBox lngCount & " items"
lbl_Exit:
Set olFolder = Nothing
Set SubFolder = Nothing
Exit Sub
End Sub