Feedback

Auslesen aller Outlook Kontakte

Sprache: C#

Mit dieser Konsolenanwendung können alle Outlook Kontakte ausgelesen werden. Dem Projekt müssen zwei Verweise hinzugefügt werden, die Referenzen werden folgendermaßen hinzugefügt: - Im Projektmappenexplorer Rechtsklick auf das betreffende Projekt - Den Tab COM auswählen - Microsoft Office 12.0 Object Library - Microsoft Outlook 12.0 Object Library Bei Office 2003 ist es die Version 11.
using System;
using Microsoft.Office.Interop.Outlook;

namespace GetContacts
{
    internal class Program
    {
        private static void Main()
        {
            var outlookApplication = new ApplicationClass();
            NameSpace mapiNamespace = outlookApplication.GetNamespace("MAPI");
            MAPIFolder contacts = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderContacts);

            for (int i = 1; i < contacts.Items.Count + 1; i++)
            {
                var contact = (ContactItem) contacts.Items[i];
                Console.WriteLine(contact.FullName);
                Console.WriteLine(contact.Email1Address);
                Console.WriteLine();
            }
            Console.Read();
        }
    }
}
using System;
using Microsoft.Office.Interop.Outlook;

namespace GetContacts
{
    internal class Program
    {
        private static void Main()
        {
            var outlookApplication = new ApplicationClass();
            NameSpace mapiNamespace = outlookApplication.GetNamespace("MAPI");
            MAPIFolder contacts = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderContacts);

            for (int i = 1; i < contacts.Items.Count + 1; i++)
            {
                var contact = (ContactItem) contacts.Items[i];
                Console.WriteLine(contact.FullName);
                Console.WriteLine(contact.Email1Address);
                Console.WriteLine();
            }
            Console.Read();
        }
    }
}

3 Kommentare

  1. Ich habe versucht das ganze mit Office 2010 (Office 14) auszuführen, es scheiterte aber an diesen Zeilen:
    [b]
    var outlookApplication = new ApplicationClass();
    NameSpace mapiNamespace = outlookApplication.GetNamespace(„MAPI“);[/b]

    Fehler 3 [b]“Microsoft.Office.Interop.Outlook.ApplicationClass“ enthält keine Definition für „GetNamespace“, und es konnte keine Erweiterungsmethode „GetNamespace“ gefunden werden, die ein erstes Argument vom Typ „Microsoft.Office.Interop.Outlook.ApplicationClass“ akzeptiert. (Fehlt eine Using-Direktive oder ein Assemblyverweis?)[/b] b:…Program.cs 127
    Fehler 2 [b]Der Interoptyp „Microsoft.Office.Interop.Outlook.ApplicationClass“ kann nicht eingebettet werden. Verwenden Sie stattdessen die entsprechende Schnittstelle. [/b]b:…Program.cs 126
    Fehler 1 [b]Für den Typ „Microsoft.Office.Interop.Outlook.ApplicationClass“ sind keine Konstruktoren definiert. [/b]b:…Program.cs 126

    Ich habe die Verweise
    [b]- Microsoft Office 14.0 Object Library
    – Microsoft Outlook 14.0 Object Library[/b]
    hinzugefügt. Was mach ich falsch?

  2. Ersetz mal die Zeile:var outlookApplication = new ApplicationClass();
    Durch:var outlookApplication = new Microsoft.Office.Interop.Outlook.Application();

    Damit funktioniert es mit Outlook 2012.

  3. Wow, das Kommentar ist schon über 2 Jahre alt ^^
    Danke für deine Hilfe, auch wenn sich das Problem mittlerweile erledigt hat.

    Rein von der Fehlermeldung her, habe ich vergessen den [b]Microsoft.Office.Interop.Outlook[/b] zu importieren ([b]using-Anweisung[/b]). Den Namespace kann man natürlich auch direkt vor jeden Typ-Namen schreiben
    Die 2. Fehlermeldung besagt, dass man in den Eigenschaften des Verweises [b]Inerop-Typ einbetten[/b] auf [b]False[/b] stellen muss.