问题
I am programming an Outlook Add-in and need to determine whether a selected email is from Inbox
or Sent Items
so that I can tag the email with folder="Inbox" or "Sent" when I save it in my database.
I understand that I can compare the folder name to Inbox or Sent Items and determine the folder, however, how do I determine when the email selected is sitting in one of the sub-folders in the inbox. Is there a FolderType
property to check whether the selected email's folder is inbox or sent (similar to identifying an item type with OlItemType
)?
回答1:
You need to look at the MailItem.Parent and cast it to a Outlook.Folder. Once you have the Folder
, you can access the display name via Folder.Name. If you want to determine whether the selected item is a subfolder of Inbox
, you would need to recursively call up 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...
You should obviously add error handling and object disposal to this sample code.
来源:https://stackoverflow.com/questions/11941288/determine-if-the-selected-email-is-from-inbox-or-sent-items