0

I need some help with VBA to count emails from 7 different subfolders of Outlook.

The result must show the number of emails in these subfolders and the date of the last email.

This subfolder is added as an extension to my Outlook only for processing data and is not my actual Outlook email. It has further subfolders inside of it which needs to be counted.

I hope someone can help me with this.

Thanks in advance

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
LindaSq95
  • 11
  • 4
  • Start here https://stackoverflow.com/questions/9076634/get-reference-to-additional-inbox. If the shared mailbox is visible in your profile then https://stackoverflow.com/a/9077144/1571407. Reference each of the seven folders separately by walking one more step of the folder tree https://stackoverflow.com/questions/8322432/using-visual-basic-to-access-subfolder-in-inbox. Now apply Count and then Sort as described in the answer post. – niton Feb 07 '22 at 11:57
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 10 '22 at 16:41

1 Answers1

0

It seems you need to iterate over all folder recursively because the nesting level is unknown. The following code iterates over all folders in Outlook recursively:

sub ProcessFolders(Folders)
  for each folder in Folders
    if Folder.DefaultItemType = olMailItem Then
       Debug.Print "---------  " & folder.Name
    End If
    ProcessFolders(folder.Folders)
  next
end sub

Here is how you could invoke it:

ProcessFolders(Application.Session.Folders)

The result must show the number of emails in these subfolders and the date of the last email.

The Items.Count property returns a long indicating the count of objects in the specified collection of folder. For example:

folderInstance.Items.Count

To find the date of last email (I suppose the last received one) you need to sort the collection using the Items.Sort method which orts the collection of items by the specified property. The index for the collection is reset to 1 upon completion of this method.

Set myItems = myFolder.Items 
myItems.Sort "[ReceivedTime]", True

MsgBox myItems(1).ReceivedTime 
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Hey this didn't seem to work. So the shared inbox is called 'A' then it further has an inbox called 'Inbox' and then two subfolders 'B' and 'C'. 'B' has three subfolders, lets say 'D', 'E', 'F'. and 'C' has a subfolder named 'G' and five more subfolders "H, I, J, K, L". I want to count the number of emails in "D, E, F" and "H, I, J, K, L". It would be great if you could help me because I have been stuck in this position and don't seem to make any progress. I am a total newbie so a full code would be great because I make blunders while trying to edit the codes. – LindaSq95 Feb 07 '22 at 09:54