Kann Guss COM-Objekt - Microsoft Outlook & C #
-
09-10-2019 - |
Frage
Ich habe diesen Code geschrieben, um die ungelesenen Elemente in meiner Outlook-Mail-Box zu sehen und hier ist der 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());
}
}
aber auf der foreach-Schleife ich diese Störung erhalten:
"Kann nicht Guss COM-Objekt des Typs‚System .__ ComObject‘to Interface-Typen‚Microsoft.Office.Interop.Outlook.MailItem‘. Dieser Vorgang ist fehlgeschlagen, da der Interface-Aufruf auf der COM-Komponente für die Schnittstelle mit IID‚{00063034 -0000-0000-C000-000000000046}‘konnte aufgrund des folgenden Fehler: Schnittstelle nicht unterstützt (Ausnahme von HRESULT: 0x80004002 (E_NOINTERFACE))."
Können Sie mir bitte helfen Sie mir, wie diese Fehler zu beheben?
Lösung
Ich musste so etwas wie Ihr Problem einer umgehen Weile zurück.
foreach (Object _obj in _explorer.CurrentFolder.Items)
{
if (_obj is MailItem)
{
MyMailHandler((MailItem)_obj);
}
}
Ich hoffe, das hilft.
Das Problem hier ist, dass _explorer.CurrentFolder.Items
mehr Objekte als nur MailItem
enthält (PostItem
ist einer von ihnen).
Andere Tipps
versuchen, den Artikel zu überprüfen, ist eine gültige mailitem
bevor seine Eigenschaften überprüft:
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());
}
}
Der folgende Code funktioniert gut, wenn ich es getestet. Aber ich muss erwähnen, dass mein Verweis „14.0-Objektbibliothek Microsoft Outlook“ zu war. Haben Sie zufällig eine andere Version benutzen?
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"); } } }
Bitte beachten Sie, dass in Outlook, viele Elemente haben einige gemeinsame Eigenschaften (zB Ablaufzeit), so können Sie, als verzweifelte Problem zu umgehen, verwenden Sie eine „dynamische“ Datentyp - entweder als Rückfallszenario für unbekannte Elementtypen oder als Standard- (solange man geht sie gut mit der Leistung getroffen).
Nice! die Lösung ein wenig angepasst, das für mich gut funktioniert
foreach (dynamic item in mailItems)
{
if (item is MailItem)
{
Response.Write("Sender: ");
Response.Write(item.SenderEmailAddress);
Response.Write(" - To:");
Response.Write(item.To);
Response.Write("<br>");
}
}