How to code in VBA to count the number of emails in shared subfolder of Outlook?

JonasAdamski

New Member
Joined
Feb 1, 2022
Messages
7
Office Version
  1. 2010
Platform
  1. Windows
Hi Guys,

I want to get the number of emails in specific folders (multiple folders, 7 to be exact), I am not worried about the date or month. But a count of the emails in these folders is enough.

I also want the date of the last email in these sub folders. The folder is a shared folder that many employees have just like I do and we are supposed to process the data from this shared folder in Outlook.

Could someone please help me because I don't seem to make any progress. Thanks and have a nice day :)


I have found this code for now, but it only counts the number of emails in a specific folder and not in multiple folders. Also this does not give the date of the last email to me.


Sub count()
Dim objItems As Outlook.Items
Dim objItem As Object
Dim objMail As Outlook.MailItem
Dim strMonth As String
Dim dReceivedTime As Date
Dim strReceivedDate As String
Dim i, n As Long
Dim strMsg As String
Dim nPrompt As Integer

Dim NS As NameSpace
Dim folder As MAPIFolder

Set NS = Application.GetNamespace("MAPI")

Set folder = NS.Folders("abcgmail.com").Folders("Inbox") '.Folders("Subfolder").Folders("Subfolder")
Set objItems = folder.Items

'Set objItems = Outlook.Application.Posteingang

objItems.SetColumns ("ReceivedTime")
strMonth = InputBox("Enter the specific month.(Format: yyyy)", "Specify year")

If strMonth <> "" Then
n = 0
For i = 1 To objItems.count
If objItems.Item(i).Class = olMail Then
Set objMail = objItems.Item(i)
dReceivedTime = objMail.ReceivedTime
strReceivedDate = Format(dReceivedTime, "yyyy")
If strReceivedDate = strMonth Then
n = n + 1
End If
End If
Next i

strMsg = "You have received " & n & " emails on " & strMonth & "."
nPrompt = MsgBox(strMsg, vbExclamation, "Count Received Emails")
Else
nPrompt = MsgBox("Please input the specific day!", vbExclamation)
End If
End Sub
 
Hey man, I tried a lot of changes but nothing seems to work. Would it be fine for you if we do a session online together on zoom or skype? Maybe tomorrow. I will also pay for your time, like 20 dollars or so. It seems like I am close but I am making stupid mistakes
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,214,822
Messages
6,121,765
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top