Retrieve Current Email Body In Outlook
c#, outlook-2007, outlook-2010, outlook-addin
Solution
You shouldn’t initialize a new `Outlook.Application()` instance each time. Most add-in frameworks provide you with an `Outlook.Application` instance, corresponding to the current Outlook session, typically through a field or property named `Application`. You are expected to use this for the lifetime of your add-in.
To get the currently-selected item, use:
Outlook.Explorer explorer = this.Application.ActiveExplorer();
Outlook.Selection selection = explorer.Selection;
if (selection.Count > 0) // Check that selection is not empty.
{
object selectedItem = selection[1]; // Index is one-based.
Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;
if (mailItem != null) // Check that selected item is a message.
{
// Process mail item here.
}
}
Note that the above will let you process the first selected item. If you have multiple items selected, you might want to process them in a loop.
Problem
in my outlook addin I want to add a button on the Ribbon so when user click this button I want to retrieve the current selected email's body , I have this code but it retrieve only the first email from the inbox because the index is 1 : ``` Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); String body = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[1]).Body; ``` so how to retrieve the current opened email in outlook? , this method work for me but I need to get the index for the current email. Thanks.