【问题标题】:Unable to cast COM object of type 'System.__ComObject'无法转换类型为“System.__ComObject”的 COM 对象
【发布时间】:2016-02-02 04:00:02
【问题描述】:

当我在下面的代码中进行强制转换时出现错误

代码:

string subject = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[temp])
    .Subject.ToString();

无法将“System.__ComObject”类型的 COM 对象转换为接口 键入“Microsoft.Office.Interop.Outlook.MailItem”。这个操作 失败,因为对 COM 组件的 QueryInterface 调用 由于 IID 为“{00063034-0000-0000-C000-000000000046}”的接口失败 出现以下错误:不支持此类接口(异常来自 HRESULT: 0x80004002 (E_NOINTERFACE))。

【问题讨论】:

  • 错误信息的其余部分是什么?
  • 我认为“没有这样的界面”
  • 现在你可以看到完整的错误信息
  • 收件箱不仅可以包含邮件项目。使用as 运算符跳过您不喜欢的那些。
  • 在你继续之前:你想做什么?我看到您正在编写一个 ASPX 页面。请注意,该代码不会与访问者的 Outlook 交互。

标签: c# outlook


【解决方案1】:

您需要先检查项目类型。 Outlook 文件夹可能包含各种类型的项目:

Object selObject = this.Application.ActiveExplorer().Selection[1];
        if (selObject is Outlook.MailItem)
        {
            Outlook.MailItem mailItem =
                (selObject as Outlook.MailItem);
            itemMessage = "The item is an e-mail message." +
                " The subject is " + mailItem.Subject + ".";
            mailItem.Display(false);
        }
        else if (selObject is Outlook.ContactItem)
        {
            Outlook.ContactItem contactItem =
                (selObject as Outlook.ContactItem);
            itemMessage = "The item is a contact." +
                " The full name is " + contactItem.Subject + ".";
            contactItem.Display(false);
        }
        else if (selObject is Outlook.AppointmentItem)
        {
            Outlook.AppointmentItem apptItem =
                (selObject as Outlook.AppointmentItem);
            itemMessage = "The item is an appointment." +
                " The subject is " + apptItem.Subject + ".";
        }
        else if (selObject is Outlook.TaskItem)
        {
            Outlook.TaskItem taskItem =
                (selObject as Outlook.TaskItem);
            itemMessage = "The item is a task. The body is "
                + taskItem.Body + ".";
        }
        else if (selObject is Outlook.MeetingItem)
        {
            Outlook.MeetingItem meetingItem =
                (selObject as Outlook.MeetingItem);
            itemMessage = "The item is a meeting item. " +
                 "The subject is " + meetingItem.Subject + ".";
        }

更多信息请参见How to: Programmatically Determine the Current Outlook Item

【讨论】:

  • 我已在收件箱中请求阅读回执类型的 Outlook 邮件,我想从 c# 阅读该特定邮件
【解决方案2】:
string subject = myInbox.Items[temp])
    .Subject.ToString();

无需先检查 myinbox 对象是否在非字符串中具有主题属性,然后如果它是字符串格式则需要对其进行强制转换,然后不需要进行强制转换。

【讨论】:

    猜你喜欢
    • 2018-06-05
    • 2018-06-20
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 2014-03-20
    相关资源
    最近更新 更多