Pages

Tuesday, January 10, 2012

Browse through all emails in Sub Folders of Specific Folder in Outlook and extract the information

If you want to run a loop through all the emails in the sub folders of specific folder in Outlook and capture the information like subject, receive date, body,etc .

Here is the code-


Dim oitem As Outlook.MailItem
Sub browse_all_emails_in_all_subfolders_of_specific_folder()
'Tools Reference Microsoft Outlook
Dim olapp As Outlook.Application
Dim olappns As Outlook.Namespace
Dim oinbox As Outlook.Folder
Dim oFolder As Outlook.MAPIFolder
'tools->refrence->microsoft outlook
Set olapp = New Outlook.Application
Set olappns = olapp.GetNamespace("MAPI")
Set oinbox = olappns.GetDefaultFolder(olFolderInbox)
Set oinbox = oinbox.Folders("Ashish")
For Each oitem In oinbox.Items
' u can add if condtions to filter the emails etc.
' u can add data it to excel sheet or database table
MsgBox "Mail Subject -> " & oitem.Subject
MsgBox "Sender Email Address -> " & oitem.SenderEmailAddress
MsgBox "Sender Name -> " & oitem.SenderName
MsgBox "Mail Body -> " & oitem.Body
MsgBox "Recived Date -> " & oitem.ReceivedTime
MsgBox oinbox.Name
MsgBox oinbox.FolderPath
Next
For Each oFolder In oinbox.Folders
Call subfolders_go(oFolder)
Next
End Sub
Private Sub subfolders_go(oParent As Outlook.Folder)
Dim oFolder1 As Outlook.MAPIFolder
For Each oitem In oParent.Items
' u can add if condtions to filter the emails etc.
' u can add data it to excel sheet or database table
MsgBox "Mail Subject -> " & oitem.Subject
MsgBox "Sender Email Address -> " & oitem.SenderEmailAddress
MsgBox "Sender Name -> " & oitem.SenderName
MsgBox "Mail Body -> " & oitem.Body
MsgBox "Recived Date -> " & oitem.ReceivedTime
MsgBox oParent.Name
MsgBox oParent.FolderPath
Next
If (oParent.Folders.Count > 0) Then
For Each oFolder1 In oParent.Folders
Call subfolders_go(oFolder1)
Next
End If
End Sub

2 comments:

  1. I get a compile error: User-Defined Type Not Defined and it highlights the section: oitem As Outlook.MailItem

    Help Please!

    ReplyDelete
  2. @ john Please add Microsoft outlook in tools- > references

    ReplyDelete