无法投射COM对象 – Microsoft Outlook和C#

前端之家收集整理的这篇文章主要介绍了无法投射COM对象 – Microsoft Outlook和C#前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我写了这个代码来查看我的Outlook邮箱中未读的项目,这里是代码
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循环中,我得到这个错误

“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)).”

你能协助我如何解决这个错误

我不得不绕过像你这样的问题一样.
foreach (Object _obj in _explorer.CurrentFolder.Items)
        {
            if (_obj is MailItem)
            {
                 MyMailHandler((MailItem)_obj);
            }
        }

希望有帮助.

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

原文链接:https://www.f2er.com/windows/365465.html

猜你在找的Windows相关文章