无法转换 COM 对象 - Microsoft Outlook &C# [英] Unable to cast COM object - Microsoft outlook & C#

查看:25
本文介绍了无法转换 COM 对象 - Microsoft Outlook &C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了此代码来查看 Outlook 邮箱中的未读项目,代码如下:

I have written this code to view the unread items in my outlook mail box and here is the code:

 Microsoft.Office.Interop.Outlook.Application app;
 Microsoft.Office.Interop.Outlook.Items items; 
 Microsoft.Office.Interop.Outlook.NameSpace ns; 
 Microsoft.Office.Interop.Outlook.MAPIFolder inbox;

 Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
        app = application;
        ns =  application.Session;
        inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
        items = inbox.Items;
        foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
        {
            if (mail.UnRead == true)
            {
                MessageBox.Show(mail.Subject.ToString());
            }
        }

但在 foreach 循环中我收到此错误:

but on the foreach loop I am getting this error:

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

"Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

你能帮我解决这个错误吗?

Can you please assist me how to resolve this error?

推荐答案

不久前我不得不解决类似您的问题的问题.

I had to get around something like your problem a while back.

        foreach (Object _obj in _explorer.CurrentFolder.Items)
        {
            if (_obj is MailItem)
            {
                 MyMailHandler((MailItem)_obj);
            }
        }

希望有所帮助.

这里的问题是 _explorer.CurrentFolder.Items 可以包含更多的对象,而不仅仅是 MailItem(PostItem 就是其中之一).

The issue here is that _explorer.CurrentFolder.Items can contain more objects than just MailItem (PostItem being one of them).

这篇关于无法转换 COM 对象 - Microsoft Outlook &C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆