Anzeige
Archiv - Navigation
1488to1492
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Outlook Kontakt in Excel einlesen

Outlook Kontakt in Excel einlesen
05.05.2016 14:30:16
Jens
Hallo,
ich habe den nachfolgenden Code zum Auslesen von Kontaktdaten aus Excel.
Jedoch wird nur der erste Kontakt ausgelesen und sobald die E-MailAdresse ausgelesen werden soll, kommt der Fehler: Laufzeitfehler 287
Wenn ich aber den E-Mail Import auskommentiere geht es.
Weis jemand warum ich die E-Maildresse nicht einlesen kann?
Sub ContactsToExcel()
Worksheets("Auslesen").Unprotect Password:="sperl"
'Deklaration
Dim oApp As New Outlook.Application
Dim nspMapi As Outlook.Namespace
Dim folMapi As Outlook.MAPIFolder
Dim itmAll As Outlook.Items
Dim itmReal As Outlook.Items
Dim itmContacts As Outlook.ContactItem
Dim strContactFilter As String
Dim excApp As Object
Dim excWkb As Object
Dim excWks As Object
Dim intRow As Integer
'Outlook-Objekte öffnen
Set nspMapi = oApp.GetNamespace("MAPI")
'Set folMapi = nspMapi.GetDefaultFolder(olFolderContacts)
Set folMapi = nspMapi.GetDefaultFolder(olFolderContacts).Folders(Sheets(2).Range("H6"). _
Value)
Set itmAll = folMapi.Items
'Verteilerlisten herausfiltern,
'nur 'Richtige Kontakte' verwenden
strContactFilter = "[MessageClass] = 'IPM.Contact'"
Set itmReal = itmAll.Restrict(strContactFilter)
'Excel-Objekte öffnen
Set excApp = Excel.Application
Set excWkb = excApp.ThisWorkbook           'Neues Workbook anlegen
Set excWks = excWkb.Sheets(2)                  'Erstes Sheet
'Excel-Worksheet aufbereiten
With excWks
'Sheet-Name
.Name = "Auslesen"
'Spaltenüberschriften
'.Cells(1, 1).Value = "Vorname"
'.Cells(1, 2).Value = "Nachname"
'.Cells(1, 3).Value = "Strasse"
'.Cells(1, 4).Value = "PLZ"
'.Cells(1, 5).Value = "Ort"
'.Cells(1, 6).Value = "Land"
'.Cells(1, 7).Value = "Telefon"
'Spaltenüberschriften fett
'.Rows("1:1").Font.Bold = True
'Outlook-Kontakte nach Excel übertragen
intRow = 9
For Each itmContacts In itmReal
.Cells(intRow, 1).Value = itmContacts.FirstName
.Cells(intRow, 2).Value = itmContacts.LastName
.Cells(intRow, 3).Value = itmContacts.CompanyName
.Cells(intRow, 4).Value = itmContacts.JobTitle
.Cells(intRow, 5).Value = itmContacts.BusinessAddressStreet
.Cells(intRow, 6).Value = itmContacts.BusinessAddressPostalCode
.Cells(intRow, 7).Value = itmContacts.BusinessAddressCity
.Cells(intRow, 8).Value = itmContacts.BusinessAddressCountry
.Cells(intRow, 9).Value = itmContacts.BusinessFaxNumber
.Cells(intRow, 10).Value = itmContacts.BusinessTelephoneNumber
.Cells(intRow, 11).Value = itmContacts.MobileTelephoneNumber
.Cells(intRow, 12).Value = itmContacts.Email1Address
intRow = intRow + 1
Next itmContacts
'Optimale Spaltenbreite
.Columns.AutoFit
End With
'Excel einblenden
excApp.Visible = True
'Speicher freigeben
Set itmReal = Nothing
Set itmAll = Nothing
Set folMapi = Nothing
Set nspMapi = Nothing
Set excWks = Nothing
Set excWkb = Nothing
Set excApp = Nothing
Worksheets("Auslesen").Protect Password:="sperl"
End Sub

3
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Outlook Kontakt in Excel einlesen
06.05.2016 09:45:45
firmus
Hallo Jens,
ich vermute, dass bei Deinen Kontakten Contacts und DistLists vorhanden sind.
Die DistLists müssen ausgefiltert werden, da sie andere Attribute haben.
Ich habe das über eine einfache Schleife realisiert, mit Class = olContact ?


If MyFolderContacts.Items.Count > 0 Then
For L = 1 To MyFolderContacts.Items.Count
If MyFolderContacts.Items(L).Class = olContact Then
Set Kontakt = MyFolderContacts.Items(L)
'For Each Kontakt In MyFolderContacts.Items


Gruß
Firmus

Anzeige
AW: Outlook Kontakt in Excel einlesen
06.05.2016 12:59:57
Jens
Hallo,
besten Dank für die INfo.
und wie binde ich das Ganze in meinem Code ein?
Gruß

AW: Outlook Kontakt in Excel einlesen
06.05.2016 17:35:16
firmus
Hallo Jens,
so ist es bei mir erfolgreich durchgelaufen - allerdings alle meine Kontakte waren Class=olcontact.


Option Explicit
Sub ContactsToExcel()
' Worksheets("Auslesen").UnProtect Password:="sperl"
'Deklaration
Dim oApp As New Outlook.Application
Dim nspMapi As Outlook.NameSpace
Dim folMapi As Outlook.MAPIFolder
Dim itmAll As Outlook.Items
Dim itmReal As Outlook.Items
Dim itmContacts As Outlook.ContactItem
Dim strContactFilter As String
Dim excApp As Object
Dim excWkb As Object
Dim excWks As Object
Dim intRow As Long, k As Long
'Outlook-Objekte öffnen
Set nspMapi = oApp.GetNamespace("MAPI")
Set folMapi = nspMapi.GetDefaultFolder(olFolderContacts)
Set itmAll = folMapi.Items
'Verteilerlisten herausfiltern,
'nur 'Richtige Kontakte' verwenden
' strContactFilter = "[MessageClass] = 'IPM.Contact'"
' Set itmReal = itmAll.Restrict(strContactFilter)
Set itmReal = folMapi.Items
'============================================================================
Set excApp = Nothing
Set excApp = New Excel.Application
excApp.Visible = True 'debug
excApp.ScreenUpdating = False
excApp.Workbooks.Add
' ActiveWorkbook.SaveAs FileName:="WB" & O200ShName save is not needed
Set excWkb = excApp.ActiveWorkbook
If excWkb.Sheets.count > 1 Then
Set excWks = excWkb.Sheets(2)
Else
Set excWks = excWkb.Sheets(1)
End If
'Excel-Worksheet aufbereiten
With excWks
.Name = "Auslesen"
'Spaltenüberschriften
.Cells(1, 1).Value = "Vorname"
.Cells(1, 2).Value = "Nachname"
.Cells(1, 3).Value = "Strasse"
.Cells(1, 4).Value = "PLZ"
.Cells(1, 5).Value = "Ort"
.Cells(1, 6).Value = "Land"
.Cells(1, 7).Value = "Telefon"
'Spaltenüberschriften fett
.Rows("1:1").Font.Bold = True
End With
'============================================================================
'Outlook-Kontakte nach Excel übertragen
intRow = 9
With excWks
For k = 1 To itmReal.count 'Each itmContacts In itmReal
If itmReal(k).Class = olContact Then
Set itmContacts = itmReal(k)
.Cells(intRow, 1).Value = itmContacts.FirstName
.Cells(intRow, 2).Value = itmContacts.LastName
.Cells(intRow, 3).Value = itmContacts.CompanyName
.Cells(intRow, 4).Value = itmContacts.JobTitle
.Cells(intRow, 5).Value = itmContacts.BusinessAddressStreet
.Cells(intRow, 6).Value = itmContacts.BusinessAddressPostalCode
.Cells(intRow, 7).Value = itmContacts.BusinessAddressCity
.Cells(intRow, 8).Value = itmContacts.BusinessAddressCountry
.Cells(intRow, 9).Value = itmContacts.BusinessFaxNumber
.Cells(intRow, 10).Value = itmContacts.BusinessTelephoneNumber
.Cells(intRow, 11).Value = itmContacts.MobileTelephoneNumber
.Cells(intRow, 12).Value = itmContacts.Email1Address
' intRow = intRow + 1
Else
intRow = intRow 'debug
End If
intRow = intRow + 1
.Cells(intRow, 13).Value = itmReal(k).Class 'abweichende Class in Zeile festhalten.
.Cells(intRow, 14).Value = k
Next k
'Optimale Spaltenbreite
.Columns.AutoFit
End With
'Excel einblenden
excApp.Visible = True
excApp.ScreenUpdating = True
'Speicher freigeben
Set itmReal = Nothing
Set itmAll = Nothing
Set folMapi = Nothing
Set nspMapi = Nothing
Set excWks = Nothing
Set excWkb = Nothing
Set excApp = Nothing
' Worksheets("Auslesen").Protect Password:="sperl"
End Sub

Lass wissen ob es klappt, bin allerdings die nächsten Wochen fast nicht verfügbar.
Gruß
Firmus
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige