Search code examples
c#outlookvstooffice-interopoutlook-addin

Accessing properties of Microsoft.Office.Interop.Outlook.MailItem


I am working on an Outlook add in application, and I am unable to properly access properties of the MailItem object. For example, if I type an email address in the "To" box, the following code does not return any values in the debugger:

When I try to examine the recipients count property in the immediate window, I get the following error:

mailItem.Recipients.Count

'System.Linq.ParallelEnumerable.Count(System.Linq.ParallelQuery, System.Func)' is a 'method', which is not valid in the given context

How can I get access to the properties?


Solution

  • The command window or immediate window see the Recipients collection as an object, not a specific Outlook embedded type (Outlook.Recipients). The only way to resolve this is to make the type dynamic before accessing its members.

    ((object)MailItem.Recipients).Count generates the error:

    (`System.Linq.ParallelEnumerable.Count(System.Linq.ParallelQuery, System.Func)' is a 'method', which is not valid in the given context).

    ((Outlook.Recipients)MailItem.Recipients).Count generates the following error advising you to use dynamic:

    Embedded interop type 'Microsoft.Office.Interop.Outlook.Recipients' is defined in both 'MyOutlookAddIn.dll' and 'Outlook.dll'. Some operations on objects of this type are not supported while debugging. Consider casting this object to type 'dynamic' when debugging or building with the 'Embed Interop Types' property set to false.

    This is what you are after to access the ComObjects Outlook.Recipients properties:

    ((dynamic)MailItem.Recipients).Count