(Solved) – Excel Macro to import email from a shared mailbox subfolder too, and mark the foldername in Excel


I have the following macro, where I’m importing emails from outlook. The macro only imports the emails from the Inbox folder,

I would like the macro to go through al the subfolders of Inbox Folder (so no Sent items, etc, but the sub folders of the Inbox folder only).

Sub GetFromOutlook()

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Dim objMail As Outlook.MailItem
Dim objFlaggedMail As Outlook.MailItem

Application.ScreenUpdating = False

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")

Set olShareName = OutlookNamespace.CreateRecipient("shared_mailbox_name")
Set Folder = OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox)


Range("A3").Value = "Subject"
Range("B3").Value = "Date"
Range("C3").Value = "Sender"
Range("D3").Value = "Category"
Range("E3").Value = "Mailbox"
i = 4

On Error Resume Next
For Each OutlookMail In Folder.Items

    Range("A" & i).Value = OutlookMail.Subject
    Range("B" & i).Value = OutlookMail.ReceivedTime
    Range("C" & i).Value = OutlookMail.SenderName
    Range("D" & i).Value = OutlookMail.Categories
    Range("E" & i).Value = OutlookMail.Folder

and in the E column, I would like if the name of the folder it took the email from would-be written…

So it is already okay with other columns, however in this way, I would like if it is copied from Inbox folder then in E column it will write Inbox, but if it is copied from Subfolder1 then it will write SUbfolder1 and etc…

How should I approach this?

Leave a Reply

Your email address will not be published. Required fields are marked *