THE_NEW_XL_GUY
New Member
- Joined
- Dec 20, 2017
- Messages
- 47
Guys need some suggestions/idea on how to get all mails from inbox folder(including sub folders-folders) of shared-mailbox.
Current code I have will get me only inbox folder mails.
Is there a way to get all the mails from inbox, other folders, sub folders of shared mailbox.
thanks in advance.
Current code I have will get me only inbox folder mails.
Is there a way to get all the mails from inbox, other folders, sub folders of shared mailbox.
thanks in advance.
Code:
Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Outlook.Namespace
Dim olShareName As Outlook.Recipient
Dim Folder As MAPIFolder
Dim olItems As Outlook.Items
Dim OutlookMail As Variant
Dim arrResults() As Variant
Dim ItemCount As Long
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set olShareName = OutlookNamespace.CreateRecipient("email@email.com")
Set Folder = OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox)
Set olItems = Folder.Items.Restrict("[ReceivedTime] >= '" & Range("From_date").Value & "' and [ReceivedTime] <= '" & Range("to_date").Value & "'")
If olItems.Count > 0 Then
ReDim arrResults(1 To olItems.Count, 1 To 5)
ItemCount = 0
For Each OutlookMail In olItems
ItemCount = ItemCount + 1
arrResults(ItemCount, 1) = OutlookMail.Subject
arrResults(ItemCount, 2) = OutlookMail.ReceivedTime
arrResults(ItemCount, 3) = OutlookMail.SenderName
arrResults(ItemCount, 4) = OutlookMail.Size
arrResults(ItemCount, 5) = OutlookMail.Categories
Next OutlookMail
Worksheets("import").Range("A5").Resize(UBound(arrResults, 1), 5) = arrResults
Else
MsgBox "No items found!", vbExclamation
End If
Set olItems = Nothing
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set olShareName = Nothing
Set OutlookApp = Nothing
End Sub