2010-09-28 83 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

相關問題