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

前端之家收集整理的这篇文章主要介绍了无法投射COM对象 – Microsoft Outlook和C#前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我写了这个代码来查看我的Outlook邮箱中未读的项目,这里是代码
  1. Microsoft.Office.Interop.Outlook.Application app;
  2. Microsoft.Office.Interop.Outlook.Items items;
  3. Microsoft.Office.Interop.Outlook.NameSpace ns;
  4. Microsoft.Office.Interop.Outlook.MAPIFolder inBox;
  5.  
  6. Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
  7. app = application;
  8. ns = application.Session;
  9. inBox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInBox);
  10. items = inBox.Items;
  11. foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
  12. {
  13. if (mail.UnRead == true)
  14. {
  15. MessageBox.Show(mail.Subject.ToString());
  16. }
  17. }

但是在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)).”

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

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

希望有帮助.

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

猜你在找的Windows相关文章