问题
I want to get all selected emails in my Outlook 2010 add-in. I found this MSDN tutorial, but I am a beginner at C#, and I don't quite understand this line of code: Object selObject = this.Application.ActiveExplorer().Selection[3];
I believe Selection[]
is something like overridden operator, indexer in C#. But, is there any way to see implementation of it? If I go through the code, I only see interfaces but not implementations. So I don't know the structure of the Selection object. What is really behind the operator []
.
Also, why do the selected items begin at index 1 and not 0?
回答1:
That lines retrieves the third selected message.
Selection[] is equivalent to Selection.Item() - Item function is marked as the indexed property accessor.
You cannot see the implementation - it is all in the Outlook Object Model.
All Outlook collections begin with 1, not 0. This is how it used to be in VB, so the Outlook Object Model uses the same convention.
回答2:
I know it's a little late but this question ranks highly in search engines. Here is the solution I use to get selected emails in Outlook Interop:
internal static IEnumerable<MailItem> GetSelectedEmails()
{
foreach (MailItem email in new Microsoft.Office.Interop.Outlook.Application().ActiveExplorer().Selection)
{
yield return email;
}
}
来源:https://stackoverflow.com/questions/14813402/c-sharp-outlook-add-in-get-selected-emails