简体   繁体   中英

Counting Sub Folder Outlook Emails through VBA

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

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM