none
Outlook - Adressen exportieren RRS feed

  • Frage

  • Hoi Freunde,

    ich möchte aus meiner Anwenung heraus Adressen, die im OL in der Kategorie Privat gelistet sind in eine csv-Datei exportieren.

    Das Format soll im Ergebnis so aussehen:
    "Name","Telefonnummer","PLZ mit Ort"

    In einem anderen Fred hatte ich das schon mit eurer Hilfe mit den Terminen lösen können, aber mit den Adressen (auf Basis der anderen Lösung) komme ich zu keinem Ergebnis.

    Wahrschienlich sind es wieder mal die genaueren Parameter auf die es im Objekt Outlook.Contact ankommt.
    Habe zwar wie vorher schon die VBAOL11.chm durchforstet, aber auch nach tagelanger Testerei tut sich da nüschte.

    Über einen erneuten Denkanstoss zu dem Thema wäre ich euch sehr dankbar.

    Doei
    Franz

    Donnerstag, 17. September 2009 09:06

Antworten

  • Hallo Franz,

    auf Kontakt-Ordner in Outlook greifst du folgendermaßen zu:

    Imports Outlook = Microsoft.Office.Interop.Outlook
    ...

    Dim anwendung As Outlook.Application = New Microsoft.Office.Interop.Outlook.Application()
    Dim ordner As Outlook.Folder = CType(anwendung.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts), Outlook.Folder)
    Dim meinOrdner As Outlook.Folder = CType(ordner.Folders.Item("NameDesOrdners"), Outlook.Folder)
    ...

    Nun kannst du die Items-Auflistung von meinOrdner durchlaufen (Items sind vom Typ Outlook.ContactItem) und die gewünschten Eigenschaften auslesen.

    Gruß
    Jan

    • Bearbeitet Jan Tittel Donnerstag, 17. September 2009 10:35 Falsche Formatübernahme der Webanwendung
    • Als Antwort markiert Robert Breitenhofer Freitag, 18. September 2009 14:17
    Donnerstag, 17. September 2009 10:33
  • Hallo Franz,

    Schau dir mal den folgenden Kode schnipsel an. Dieser exportiert die Einträge aus dem Contact Ordner in eine csv-Datei.

    Nicht vergessen die Microsoft.Office.Interop.Outlook Referenz einzufügen. J


    Imports Microsoft.Office.Interop
    Imports System.IO
    
    Public Class Form1
        Dim outlookApp As Microsoft.Office.Interop.Outlook.Application
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim outlookNameSpace As Outlook.NameSpace = outlookApp.GetNamespace("MAPI")
            Dim contactFolder As Outlook.MAPIFolder = _
                outlookNameSpace.GetDefaultFolder( _
                Outlook.OlDefaultFolders.olFolderContacts)
            Dim contactItems As Outlook.Items = contactFolder.Items
            Dim sw As StreamWriter = New StreamWriter("C:\Robert\Mail.csv", True)
            Dim str As String = String.Empty
            Try
                For Each contact As Outlook.ContactItem In contactItems
                    If (contact.Account IsNot Nothing) Then
                        str += contact.Account
                    Else
                        str += String.Empty
                    End If
                    If (contact.FirstName IsNot Nothing) Then
                        str += "," & contact.FirstName
                    Else
                        str += "," & String.Empty
                    End If
                    If (contact.LastName IsNot Nothing) Then
                        str += "," & contact.LastName
                    Else
                        str += "," & String.Empty
                    End If
                    If (contact.BusinessTelephoneNumber IsNot Nothing) Then
                        str += "," & contact.BusinessTelephoneNumber
                    Else
                        str += "," & String.Empty
                    End If
                    If (contact.BusinessAddress IsNot Nothing) Then
                        str += "," & contact.BusinessAddress
                    Else
                        str += "," & String.Empty
                    End If
                    If (contact.CompanyName IsNot Nothing) Then
                        str += "," & contact.CompanyName
                    Else
                        str += "," & String.Empty
                    End If
                    sw.WriteLine(str)
                    str = String.Empty
                Next
                sw.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            outlookApp = New Microsoft.Office.Interop.Outlook.Application()
        End Sub
    End Class




    Für Deine Referenz lies auch diese Artikel über Automatisierung.

    1. Programming samples that can reference items and folders in Outlook by using Visual Basic .NET
    2. Handy Tasks Using Microsoft Office Outlook 2003 and Visual Basic .NET
    3. How to use the Microsoft Outlook Object Library to force a Send/Receive action by using Visual Basic .NET


    Grüße,

    Robert

    Donnerstag, 17. September 2009 11:37

Alle Antworten

  • Hallo Franz,

    auf Kontakt-Ordner in Outlook greifst du folgendermaßen zu:

    Imports Outlook = Microsoft.Office.Interop.Outlook
    ...

    Dim anwendung As Outlook.Application = New Microsoft.Office.Interop.Outlook.Application()
    Dim ordner As Outlook.Folder = CType(anwendung.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts), Outlook.Folder)
    Dim meinOrdner As Outlook.Folder = CType(ordner.Folders.Item("NameDesOrdners"), Outlook.Folder)
    ...

    Nun kannst du die Items-Auflistung von meinOrdner durchlaufen (Items sind vom Typ Outlook.ContactItem) und die gewünschten Eigenschaften auslesen.

    Gruß
    Jan

    • Bearbeitet Jan Tittel Donnerstag, 17. September 2009 10:35 Falsche Formatübernahme der Webanwendung
    • Als Antwort markiert Robert Breitenhofer Freitag, 18. September 2009 14:17
    Donnerstag, 17. September 2009 10:33
  • Hallo Franz,

    Schau dir mal den folgenden Kode schnipsel an. Dieser exportiert die Einträge aus dem Contact Ordner in eine csv-Datei.

    Nicht vergessen die Microsoft.Office.Interop.Outlook Referenz einzufügen. J


    Imports Microsoft.Office.Interop
    Imports System.IO
    
    Public Class Form1
        Dim outlookApp As Microsoft.Office.Interop.Outlook.Application
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim outlookNameSpace As Outlook.NameSpace = outlookApp.GetNamespace("MAPI")
            Dim contactFolder As Outlook.MAPIFolder = _
                outlookNameSpace.GetDefaultFolder( _
                Outlook.OlDefaultFolders.olFolderContacts)
            Dim contactItems As Outlook.Items = contactFolder.Items
            Dim sw As StreamWriter = New StreamWriter("C:\Robert\Mail.csv", True)
            Dim str As String = String.Empty
            Try
                For Each contact As Outlook.ContactItem In contactItems
                    If (contact.Account IsNot Nothing) Then
                        str += contact.Account
                    Else
                        str += String.Empty
                    End If
                    If (contact.FirstName IsNot Nothing) Then
                        str += "," & contact.FirstName
                    Else
                        str += "," & String.Empty
                    End If
                    If (contact.LastName IsNot Nothing) Then
                        str += "," & contact.LastName
                    Else
                        str += "," & String.Empty
                    End If
                    If (contact.BusinessTelephoneNumber IsNot Nothing) Then
                        str += "," & contact.BusinessTelephoneNumber
                    Else
                        str += "," & String.Empty
                    End If
                    If (contact.BusinessAddress IsNot Nothing) Then
                        str += "," & contact.BusinessAddress
                    Else
                        str += "," & String.Empty
                    End If
                    If (contact.CompanyName IsNot Nothing) Then
                        str += "," & contact.CompanyName
                    Else
                        str += "," & String.Empty
                    End If
                    sw.WriteLine(str)
                    str = String.Empty
                Next
                sw.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            outlookApp = New Microsoft.Office.Interop.Outlook.Application()
        End Sub
    End Class




    Für Deine Referenz lies auch diese Artikel über Automatisierung.

    1. Programming samples that can reference items and folders in Outlook by using Visual Basic .NET
    2. Handy Tasks Using Microsoft Office Outlook 2003 and Visual Basic .NET
    3. How to use the Microsoft Outlook Object Library to force a Send/Receive action by using Visual Basic .NET


    Grüße,

    Robert

    Donnerstag, 17. September 2009 11:37
  • Hoi Robert & Jan,

    vielen Dank - das hat so prinzipiell erstmal funktioniert :-)
    Habe mal ein paar Daten geändert (z.B. Fullname anstatt Vorname und Nachname) usw., klappt alles wunderbar.


    Wie bekomme ich die Ausgabe vorher noch in eine Listbox oder Textbox ?
    Das wäre zur Kontrolle vor dem Speichern nochmal echter Luxus.

    Doei
    Franz


    • Bearbeitet Trixi-N Donnerstag, 17. September 2009 17:58 Edit
    Donnerstag, 17. September 2009 17:54
  • Hoi,

    manchmal bin ich auch doof ;-)
    Habe das natürlich selber "gemanagt".

    Doei
    Franz
    Donnerstag, 17. September 2009 20:00
  • Hallo Franz,

    Es freut uns dass Du Dein Problem lösen könntest. Vielen Dank auch an Jan für die Konstruktive Diskussion.

    Grüße,

    Robert

    Freitag, 18. September 2009 14:17