2011-01-11 17 views
12

C# kodu:COM nesne artığını açılamıyor Microsoft görünüm & burada benim bakış posta kutusuna okunmamış öğeleri görüntülemek için bu kodu yazılı ve adres

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()); 
      } 
     } 

ama foreach döngüsü üzerinde bu soruyla hata:

"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))."

Bu hatayı nasıl çözebilirim?

+0

bu bir eklenti var? – Bolu

+0

@Bolu Hayır, bu benim C# Windows uygulamasında yazdığım şey – Zerotoinfinity

+1

MAPIFolder kullanımdan kaldırıldı, bunun yerine Klasör'ü kullanın. –

cevap

19

Bir süre önce probleminiz gibi bir şeylere kapıldım.

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

Bu yardımcı olur umarım.

Buradaki sorun, _explorer.CurrentFolder.Items'un yalnızca MailItem'dan (PostItem bunlardan biri) daha fazla nesne içerebileceğidir.

+0

ile Outlook 2016 ile çalıştım, aşağıdaki satırda aynı hatayı alıyorum Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook .Uygulama(); – Meer

3

Aşağıdaki kod, sınandığımda iyi çalıştı. Ancak, referansımın "Microsoft Outlook 14.0 Nesne Kitaplığı" için olduğunu belirtmeliyim. Başka bir versiyon kullanıyor musunuz? bilinmeyen madde türleri için geri dönüş senaryo olarak ya - Eğer umutsuz çözüm olarak, bir "dinamik" veri türü kullanabilmesi

 
    public class Outlook 
    { 
    readonly Microsoft.Office.Interop.Outlook.Items  _items; 
    readonly Microsoft.Office.Interop.Outlook.NameSpace _ns; 
    readonly Microsoft.Office.Interop.Outlook.MAPIFolder _inbox; 
    readonly Microsoft.Office.Interop.Outlook.Application _application = new Microsoft.Office.Interop.Outlook.Application(); 

    public Outlook() 
    { 
     _ns = _application.Session; 
     _inbox = _ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); 
     _items = _inbox.Items; 

     foreach (var item in _items) 
     { 
      string subject= string.Empty; 
      var mail = item as Microsoft.Office.Interop.Outlook.MailItem; 
      if (mail != null) 
       var subject = mail.Subject; 
      else 
       Debug.WriteLine("Item is not a MailItem"); 
     } 
    } 
    } 

Outlook lütfen unutmayın, birçok ürün, (örneğin son kullanma süresi) bazı ortak özellikleri vardır veya varsayılan olarak (performans isabetiyle iyi olduğunuz sürece).

+0

Aynı şeyi denedim ve bu iletiyi aldım: 'Microsoft.Office.Interop.Outlook.PostItem' arabirimine 'System .__ ComObject' türünde COM nesnesi dönüştürülemiyor. IID '{00063024-0000-0000-C000-000000000046}} ile arabirimin COM bileşenindeki QueryInterface çağrısı, aşağıdaki hata nedeniyle başarısız olduğundan bu işlem başarısız oldu: Böyle bir arabirim desteklenmedi (HRESULT özel durum: 0x80004002 (E_NOINTERFACE)) . – Zerotoinfinity

+0

En son Microsoft.Office.Interop.Outlook.dll – Ben

6

özelliklerini kontrol etmeden önce öğesinin geçerli mailitem olduğunu kontrol etmek deneyin:

foreach (Object mail in items) 
{ 
    if ((mail as Outlook.MailItem)!=null && (mail as Outlook.MailItem).UnRead == true) 
    { 
     MessageBox.Show((mail as Outlook.MailItem).Subject.ToString()); 
    } 
} 
İlgili konular