Showing posts with label Accessing sub folders in outlook using vba. Show all posts
Showing posts with label Accessing sub folders in outlook using vba. Show all posts

Monday, September 20, 2021

Accessing sub folders in outlook using vba

 

 here I am trying to access a folder named test under inbox

 

 

 

 

 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("Inbox").Folders("test")

'Set olFolder = olFolder.Folders("test")

For Each msg In olFolder.Items
   ' MsgBox msg.Subject
Next

'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