Determine if the selected email is selected from inbox or sent items

I am programming an Outlook add-in and must determine if the selected email is from Inbox or Sent Items so that I can mark the email with the folder = "Inbox" or "Inbox, Sent" when I save it in my database.

I understand that I can compare the name of the folder with the Inbox or Sent Items folder and determine the folder, however, how to determine when the selected email address is in one of the subfolders in the Inbox folder. Is there a FolderType property to check if the selected Inbox is selected or sent (similar to identifying an item type using OlItemType )?

+4
source share
1 answer

You need to look at MailItem.Parent and pass it to Outlook.Folder . When you have a Folder , you can access the display name using Folder.Name . If you want to determine if the selected item is an Inbox subfolder, you will need to recursively call the Parent tree until Parent is null to find the root parent folder.

 Outlook.Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer(); Outlook.MailItem mailItem = explorer.Selection.OfType<Outlook.MailItem>().First(); Outlook.Folder parentFolder = mailItem.Parent as Outlook.Folder; if (parentFolder.Parent == null) // we are at the root { string folderName = parentFolder.Name; } else // .. recurse up the parent tree casting parentFolder.Parent as Outlook.Folder... 

Obviously, you should add error handling and object removal to this code sample.

+5
source

All Articles