无法转换'System .__ ComObject'类型的COM对象

时间:2015-11-02 09:46:27

标签: c# outlook

enter image description here

enter image description here当我在代码下面进行投射时,我收到了错误

代码:

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

无法将“System .__ ComObject”类型的COM对象强制转换为接口   输入'Microsoft.Office.Interop.Outlook.MailItem'。这个操作   失败,因为QueryInterface调用了COM组件   与IID'{00063034-0000-0000-C000-000000000046}'的接口失败   出现以下错误:未支持此类接口(例外情况)   HRESULT:0x80004002(E_NOINTERFACE))。

2 个答案:

答案 0 :(得分:3)

您需要先检查项目类型。 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

答案 1 :(得分:0)

string subject = myInbox.Items[temp])
    .Subject.ToString();

没有必要使用frist检查myinbox对象的主题属性是非字符串,然后如果它是字符串格式,则需要将其强制转换,然后不需要强制转换它。

相关问题