views:

34

answers:

1

I'm trying to create a mail item and add some attachments to it using late binding. I've already managed to create the mail item, but I cannot invoke the Attachments property.

object objApp;
object objEmail;

Type objClassType = Type.GetTypeFromProgID("Outlook.Application");
objApp = Activator.CreateInstance(objClassType);

// Microsoft.Office.Interop.Outlook.OlItemType.olMailItem = 0
objEmail = objApp.GetType().InvokeMember("CreateItem", BindingFlags.InvokeMethod, null, objApp, new object[] { 0 });

mailItemType.InvokeMember("Subject", BindingFlags.SetProperty, null, objEmail, new object[] { subject });

// THIS RETURNS NULL?!
PropertyInfo att = mailItemType.GetProperty("Attachments", BindingFlags.GetProperty);

What can I do when there's no Attachments property (or method) to invoke? With early binding it's simply objEmail.Attachments.Add(...)

A: 

The problem was I called the GetProperty directly. It should be InvockeMember with BindingFlags.GetProperty. I think this is because the interface is IUnknown and only method invoking works.

I also discovered that you can get the Attachments type from CLSID

Type attachmentsType = Type.GetTypeFromCLSID(new Guid("0006303C-0000-0000-C000-000000000046"));

and then call

attachmentsType.InvokeMember("Add", BindingFlags.InvokeMethod, null, attachments, new object[] { ... });

This example is for Office 2003.

kor_