'Import E-Mails from Outlook subroutine
'Create an Outlook Application object
Dim OutlookApp As Outlook.Application
'Create an Namespace object
Dim OutlookNamespace As Namespace
'Create a Outlook folder object
'Object to store the retrieved E-Mails
Dim OutlookItems As Outlook.items
'Temporary object, used for iteration
Dim OutlookMail As Variant
'Get the folder name from excel sheet
FolderName = ActiveSheet.Range("D1").Value
'Create an instance of Outlook
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
'If the checkbox is not checked, then the folder is at the same level as inbox
If ActiveSheet.OLEObjects("check").Object.Value = False Then
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Parent.Folders(FolderName)
'If the checkbox is active, then it is a sub-folder of inbox
If ActiveSheet.OLEObjects("check").Object.Value = True Then
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).Folders(FolderName)
'Get the folder items and sort according to the recieved time
Set OutlookItems = Folder.items
OutlookItems.Sort "ReceivedTime", True
'Results counter starting from Row 5
For Each OutlookMail In OutlookItems
If OutlookMail.ReceivedTime >= ActiveSheet.Range("B1").Value Then
ActiveSheet.Cells(i, 1).Value = OutlookMail.ReceivedTime
ActiveSheet.Cells(i, 2).Value = OutlookMail.SenderName
ActiveSheet.Cells(i, 3).Value = OutlookMail.Subject
ActiveSheet.Cells(i, 4).Value = OutlookMail.Body
'Display the total number of e-mails retrieved
ActiveSheet.Range("B2").Value = i - 5
ActiveSheet.Range("B2").Font.Color = vbBlack
Set OutlookItems = Nothing
Set OutlookNamespace = Nothing
ActiveSheet.Range("B2").Value = "Folder name not found"
ActiveSheet.Range("B2").Font.Color = vbRed
Set OutlookItems = Nothing
Set OutlookNamespace = Nothing