代码之家  ›  专栏  ›  技术社区  ›  kor_

如何使用Outlook后期绑定向mailitem添加附件

  •  3
  • kor_  · 技术社区  · 14 年前

    我正在尝试创建一个邮件项目,并使用后期绑定向其添加一些附件。我已经创建了邮件项目,但无法调用 附件

    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添加(...)

    2 回复  |  直到 14 年前
        1
  •  2
  •   kor_    14 年前

    问题是我直接调用了GetProperty。它应该和会员一起开发票BindingFlags.GetProperty属性. 我认为这是因为接口是未知的,也是唯一的方法 调用

    我还发现可以从CLSID获取附件类型

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

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

    这个例子是为 办公室2003

        2
  •  0
  •   Surjit Samra    13 年前

    我认为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);