无法投COM对象 - 微软Outlook和放大器; C# [英] Unable to cast COM object - Microsoft outlook & C#

查看:211
本文介绍了无法投COM对象 - 微软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:

无法投型系统.__ ComObject的COM对象的接口类型Micr​​osoft.Office.Interop.Outlook.MailItem。此操作失败的原因是对IID{00063034-0000-0000-C000-000000000046}接口的COM组件调用QueryInterface失败,原因是以下错误:不支持此接口(异常来自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);
            }
        }



希望有所帮助。

Hope that helps.

这里的问题是, _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对象 - 微软Outlook和放大器; C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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