2010-09-28 58 views
3

我正尝试使用后期绑定来创建邮件项目并向其添加一些附件。我已经设法创建邮件项目,但我无法调用属性附件属性。如何使用Outlook后期绑定将附件添加到mailitem

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); 

当没有Attachments属性(或方法)调用时,我该怎么办?与早期绑定它只是objEmail.Attachments.Add(...)

回答

2

问题是我直接调用GetProperty。它应该是与BindingFlags.GetProperty的InvockeMember。我认为这是因为界面是IUnknown,只有方法调用的作品。

我还发现,就可以得到附件的CLSID

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

,然后调用

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

这个例子是Office 2003的

0

我认为的getProperty语句是不完全正确,我这做以下工作:

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); 
+1

为了更好的代码格式化选中所有代码,然后按Ctrl + K – 2011-12-31 14:08:33

相关问题