PDA

View Full Version : Count emails within subfolders and possibly store it in a variable.



hawan1823
10-07-2014, 10:17 AM
Just need help The code below currently counts the total # of emails. I want to edit the code to count the amount mail within subfolders which are under the inbox and possibly store it in a variable. E.g some sub folders within the inbox are Folder1, Folder2, Folder3.






Dim olApp As Outlook.Application
Dim olNs As NameSpace
Dim Fldr As MAPIFolder
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
MsgBox Fldr.Items.Count
Set Fldr = Nothing
Set olNs = Nothing
Set olApp = Nothing

westconn1
10-08-2014, 02:37 AM
if there are subfoders within subfolders, do you want to count all?
if so you would need some recursive routine

you could try like

Dim cnt As Collection
Sub allfolders(f As MAPIFolder)
Dim subf As MAPIFolder
cnt.Add f.Items.Count, f.Name
For Each subf In f.Folders
allfolders subf
Next

End Sub
Sub getall()
Dim ibox As MAPIFolder
Set cnt = New Collection
Set ibox = Session.GetDefaultFolder(olFolderInbox)
allfolders ibox
End Subeach folder will be added to the collection, indexed by name, note this particular code will error if folder structure can have multiple subfolder with same name

return results like msgbox cnt("inbox") or any other folder name