开发者

How to add attachments to mailitem using Outlook late binding

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?!
PropertyIn开发者_运维问答fo 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(...)


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.


I think the GetProperty stmt isn't quite right, I got this to work by doing the following:

object oMailItemAttachments = oMailItem.GetType().InvokeMember("Attachments", System.Reflection.BindingFlags.GetProperty, null, oMailItem, null);

parameter = new object[4];
parameter[0] = @sFileName;
parameter[1] = 1;
parameter[2] = Type.Missing;
parameter[3] = Type.Missing;

oMailItemAttachments.GetType().InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, oMailItemAttachments, parameter);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜