Sunday, September 19, 2021

Accessing a folder by name and looping through items inside it in outlook using vba

 Sub accessFolderbyName()

Dim olApp, olAccts, olInspect As Object
Set olApp = CreateObject("Outlook.Application")
Dim olFolder As Outlook.MAPIFolder
Dim count As Integer
count = 0
Set olFolder = olApp.GetNamespace("MAPI").Folders("allsourav@hotmail.com").Folders("Sent Items")
'now let us loop through the mail items in that folder

For Each itmmail In olFolder.Items

If itmmail.SentOnBehalfOfName = "allsourav@hotmail.com" Then
count = count + 1
End If
Next itmmail
MsgBox (count)

Dim itmcount As Integer
itmcount = olFolder.Items.count
MsgBox itmcount
count = 0
Dim x As Integer

'reading mail older to newer

For x = 1 To itmcount

If olFolder.Items(x).SentOnBehalfOfName = "allsourav@hotmail.com" Then
count = count + 1
End If
Next x
MsgBox (count)

count = 0
'reading mail in reverse order ,new to old

For x = itmcount To 1 Step -1

If olFolder.Items(x).SentOnBehalfOfName = "allsourav@hotmail.com" Then
count = count + 1
End If
Next x
MsgBox (count)

End Sub

No comments:

Post a Comment