[英]How to add attachments to mailitem using Outlook late binding
我正在尝试使用后期绑定创建一个邮件项目并添加一些附件。 我已经设法创建邮件项目,但我无法调用附件属性。
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);
当没有要调用的附件属性(或方法)时,我该怎么办? 通过早期绑定,它只是objEmail.Attachments.Add(...)
问题是我直接调用了GetProperty。 它应该是InvockeMember和BindingFlags.GetProperty。 我认为这是因为接口是IUnknown并且只有方法调用才有效。
我还发现您可以从CLSID获取附件类型
Type attachmentsType = Type.GetTypeFromCLSID(new Guid("0006303C-0000-0000-C000-000000000046"));
然后打电话
attachmentsType.InvokeMember("Add", BindingFlags.InvokeMethod, null, attachments, new object[] { ... });
此示例适用于Office 2003 。
我认为GetProperty stmt不太正确,我通过执行以下操作实现了这一点:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.