Get current email authority in Outlook

in my Outlook addin I want to add a button on the ribbon, so when the user clicks this button, I want to get the currently selected email object, I have this code, but it only extracts the first email from the inbox, because the index one:

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 get current open email in Outlook ?, this method works for me, but I need to get the index for the current email.

Thanks.

+7
source share
2 answers

You do not have to initialize a new instance of Outlook.Application() every time. Most add-in infrastructures provide you with an instance of Outlook.Application that matches the current Outlook session, usually through a field or property named Application . You are expected to use this for the life of your add-in.

To get the 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. } } 

Please note that the above will allow you to process the first selected item. If you have selected multiple elements, you can process them in a loop.

+7
source

Add a link to the top

 using Outlook = Microsoft.Office.Interop.Outlook; 

Then inside the method;

 Outlook._Application oApp = new Outlook.Application(); if (oApp.ActiveExplorer().Selection.Count > 0) { Object selObject = oApp.ActiveExplorer().Selection[1]; if (selObject is Outlook.MailItem) { Outlook.MailItem mailItem = (selObject as Outlook.MailItem); String htmlBody = mailItem.HTMLBody; String Body = mailItem.Body; } } 

You can also change the body that will be displayed in Outlook before viewing mail.

+6
source

All Articles