PDA

View Full Version : count Email in different Outlook Inbox with subfolders



rahul_r79
01-03-2012, 10:00 PM
Hi guys

I need some help to count Email in different outlook Email Inbox with different Sub folders in each of them.
Any help will be apprieciated.


Thanks
:banghead: :banghead: :banghead: :banghead: :banghead: :banghead:

JP2112
01-05-2012, 10:14 AM
Do you have any code written so far?

rahul_r79
01-05-2012, 05:33 PM
Sub n()
Dim appOl As New Outlook.Application
Dim ns As Outlook.NameSpace
Dim Inbox As Outlook.MAPIFolder
Dim SubFolder As Outlook.MAPIFolder
Dim Item As Object
Dim Atmt As Outlook.Attachment
Dim FileName As String
Dim i As Integer
Dim varResponse As Variant
Dim objFolder As MAPIFolder
Set ns = appOl.GetNamespace("MAPI")
Set objFolder = ns.PickFolder


Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("MyFolder")
'Dim objOutlook As Object, objnSpace As Object, objFolder As Object
'Dim EmailCount As Integer
'Set objOutlook = CreateObject("Outlook.Application")
'Set objnSpace = objOutlook.GetNamespace("MAPI")

' On Error Resume Next
' Set objFolder = objnSpace.Folders("Personal Folders").Folders("MIS")
' If Err.Number <> 0 Then
' Err.Clear
' MsgBox "No such folder."
' Exit Sub
' End If

i = SubFolder.Items.Count
' Set objFolder = Nothing
' Set objnSpace = Nothing
' Set objOutlook = Nothing
'MsgBox "Number of emails in the folder: " & i, , "MIS email count"
Dim iCount As Integer, DateCount As Integer
Dim myDate As Date
'i = objFolder.Items.Count
DateCount = 0
' myDate = Sheets("Sheet1").Range("A1").Value
myDate = "23/11/2011"
For iCount = 1 To i
With SubFolder.Items(iCount)
If DateSerial(Year(.ReceivedTime), Month(.ReceivedTime), Day(.ReceivedTime)) = myDate Then DateCount = DateCount + 1
End With
Next iCount
MsgBox "Number of emails in MIS folder with matching date: " & DateCount, , "MIS date count"

End Sub

rahul_r79
01-05-2012, 05:46 PM
The above code can count emails in my personal inbox but I have to add another email inbox which have subfolders and need to count emails in those folder

Sebastian H
01-13-2012, 03:58 PM
Why don't you just change the sub to a function and pass the folder as a parameter, like this:

Sub CountAll
MsgBox "Number of emails in MIS folder with matching date: " & , n (ns.GetDefaultFolder(olFolderInbox)) + n (your_other_folder), "MIS date count"
End Sub

Function n (Inbox As Outlook.MAPIFolder) as Integer
' rest of sub as defined above by you
n = DateCount
End Function
(I don't know why the indent doesn't work in the above text.)