[英]: 'Unable to cast COM object of type 'System.__ComObject' to interface type
[英]Unable to cast COM object of type 'System.__ComObject'
當我在下面的代碼中進行強制轉換時,出現錯誤
代碼:
string subject = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[temp])
.Subject.ToString();
無法將類型為“ System .__ ComObject”的COM對象轉換為接口類型為“ Microsoft.Office.Interop.Outlook.MailItem”。 此操作失敗,因為具有以下錯誤的IID為“ {00063034-0000-0000-C000-000000000046}”的COM組件上的COM接口上的QueryInterface調用由於以下錯誤而失敗:不支持此類接口(HRESULT的異常:0x80004002(E_NOINTERFACE)) 。
您需要先檢查項目類型。 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 + ".";
}
有關更多信息,請參見如何:以編程方式確定當前Outlook項目 。
string subject = myInbox.Items[temp])
.Subject.ToString();
無需先檢查myinbox對象是否具有subject屬性而不是string,然后如果它是string formate則需要對其進行鑄造,則無需進行鑄造。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.