| Der folgende Code wurde von Johannes Faget gepostet: Option Explicit
Public Type Contact
Categories As String LastName As String
FirstName As String
HomeAddressStreet As String
HomeAddressPostalCode As String
HomeAddressCity As String
HomeAddressCountry As String
CompanyName As String
HomeTelephoneNumber As String
BusinessTelephoneNumber As String
MobileTelephoneNumber As String
HomeFaxNumber As String
Email1Address As String
PersonalHomePage As String
End Type
Private Const olFolderContacts As Long = 10
Private Const errFile As String = "error.log"
Private Const doErrorLogging As Boolean = True
Public Function isOutlookInstalled() As Boolean
Dim olApp As Object
On Error Goto errHandler
Set olApp = CreateObject("Outlook.Application")
isOutlookInstalled = True
Set olApp = Nothing
Exit Function
errHandler:
If Err.Number = 429 Then
Else
ErrPrint "Function isOutlookInstalled() returned with error"
End If
isOutlookInstalled = False
End Function
Public Function getOutlookShortVersion() As Integer
Dim olApp As Object
Dim splittArr() As String
On Error Goto errHandler
If Not isOutlookInstalled Then
getOutlookShortVersion = 0
Else
Set olApp = CreateObject("Outlook.Application")
splittArr = Split(olApp.Version, ".")
If Not isStringArrayDimensioned(splittArr()) Then
getOutlookShortVersion = 0
Else
getOutlookShortVersion = CInt(splittArr(0))
End If
Set olApp = Nothing
End If
Exit Function
errHandler:
ErrPrint "Function getOutlookShortVersion() returned with error"
getOutlookShortVersion = 0
End Function
Public Function getOutlookVersionName() As String
On Error Goto errHandler
If Not isOutlookInstalled Then
getOutlookVersionName = vbNullString
Else
Select Case getOutlookShortVersion
Case 10
getOutlookVersionName = "Outlook® 2002"
Case 9
getOutlookVersionName = "Outlook® 2000"
Case 8
getOutlookVersionName = "Outlook® 97"
Case 7
getOutlookVersionName = "Outlook® 95"
Case Is < 7
getOutlookVersionName = "Outlook® version less than 95"
End Select
End If
Exit Function
errHandler:
ErrPrint "Function getOutlookVersionName() returned with error"
getOutlookVersionName = vbNullString
End Function
Public Function getOutlookLongVersion() As String
Dim olApp As Object
On Error Goto errHandler
If Not isOutlookInstalled Then
getOutlookLongVersion = vbNullString
Else
Set olApp = CreateObject("Outlook.Application")
getOutlookLongVersion = olApp.Version
Set olApp = Nothing
End If
Exit Function
errHandler:
ErrPrint "Function getOutlookLongVersion() returned with error"
getOutlookLongVersion = vbNullString
End Function
Public Function getContactFolderCount() As Integer
Dim olApp As Object
Dim olNameSpace As Object
Dim olFolder As Object
On Error Goto errHandler
If Not isOutlookInstalled Then
getContactFolderCount = 0
Else
Set olApp = CreateObject("Outlook.Application")
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderContacts)
getContactFolderCount = olFolder.Items.Count
Set olFolder = Nothing
Set olNameSpace = Nothing
Set olApp = Nothing
End If
Exit Function
errHandler:
ErrPrint "Function getContactFolderCount() returned with error"
getContactFolderCount = 0
End Function
Public Function getContact(index As Integer) As Contact
Dim olApp As Object
Dim olNameSpace As Object
Dim olFolder As Object
Dim olContact As Object
Dim tmpContact As Contact
On Error Goto errHandler
If Not isOutlookInstalled Then
ElseIf getContactFolderCount < index Then
Else
Set olApp = CreateObject("Outlook.Application")
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderContacts)
Set olContact = olFolder.Items(index)
With tmpContact
.BusinessTelephoneNumber = olContact.BusinessTelephoneNumber
.Categories = olContact.Categories
.CompanyName = olContact.CompanyName
.Email1Address = olContact.Email1Address
.FirstName = olContact.FirstName
.HomeAddressCity = olContact.HomeAddressCity
.HomeAddressCountry = olContact.HomeAddressCountry
.HomeAddressPostalCode = olContact.HomeAddressPostalCode
.HomeAddressStreet = olContact.HomeAddressStreet
.HomeFaxNumber = olContact.HomeFaxNumber
.HomeTelephoneNumber = olContact.HomeTelephoneNumber
.LastName = olContact.LastName
.MobileTelephoneNumber = olContact.MobileTelephoneNumber
.PersonalHomePage = olContact.PersonalHomePage
End With
Set olContact = Nothing
Set olFolder = Nothing
Set olNameSpace = Nothing
Set olApp = Nothing
getContact = tmpContact
End If
Exit Function
errHandler:
ErrPrint "Function getContact() returned with error"
End Function
Public Function modifyContact(index As Integer, _
modContact As Contact) As Boolean
Dim olApp As Object
Dim olNameSpace As Object
Dim olFolder As Object
Dim olContact As Object
On Error Goto errHandler
If Not isOutlookInstalled Then
modifyContact = False
ElseIf getContactFolderCount < index Then
modifyContact = False
Else
Set olApp = CreateObject("Outlook.Application")
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderContacts)
Set olContact = olFolder.Items(index)
With olContact
.BusinessTelephoneNumber = _
modContact.BusinessTelephoneNumber
.Categories = modContact.Categories
.CompanyName = modContact.CompanyName
.Email1Address = modContact.Email1Address
.FirstName = modContact.FirstName
.HomeAddressCity = modContact.HomeAddressCity
.HomeAddressCountry = modContact.HomeAddressCountry
.HomeAddressPostalCode = modContact.HomeAddressPostalCode
.HomeAddressStreet = modContact.HomeAddressStreet
.HomeFaxNumber = modContact.HomeFaxNumber
.HomeTelephoneNumber = modContact.HomeTelephoneNumber
.LastName = modContact.LastName
.MobileTelephoneNumber = modContact.MobileTelephoneNumber
.PersonalHomePage = modContact.PersonalHomePage
.Save
End With
Set olContact = Nothing
Set olFolder = Nothing
Set olNameSpace = Nothing
Set olApp = Nothing
modifyContact = True
End If
Exit Function
errHandler:
ErrPrint "Function modifyContact() returned with error"
modifyContact = False
End Function
Public Function addContact(newContact As Contact) As Integer
Dim olApp As Object
Dim olNameSpace As Object
Dim olFolder As Object
Dim olContact As Object
On Error Goto errHandler
If Not isOutlookInstalled Then
addContact = 0
Else
Set olApp = CreateObject("Outlook.Application")
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderContacts)
Set olContact = olFolder.Items.Add
With olContact
.BusinessTelephoneNumber = _
newContact.BusinessTelephoneNumber
.Categories = newContact.Categories
.CompanyName = newContact.CompanyName
.Email1Address = newContact.Email1Address
.FirstName = newContact.FirstName
.HomeAddressCity = newContact.HomeAddressCity
.HomeAddressCountry = newContact.HomeAddressCountry
.HomeAddressPostalCode = newContact.HomeAddressPostalCode
.HomeAddressStreet = newContact.HomeAddressStreet
.HomeFaxNumber = newContact.HomeFaxNumber
.HomeTelephoneNumber = newContact.HomeTelephoneNumber
.LastName = newContact.LastName
.MobileTelephoneNumber = newContact.MobileTelephoneNumber
.PersonalHomePage = newContact.PersonalHomePage
.Save
End With
Set olContact = Nothing
Set olFolder = Nothing
Set olNameSpace = Nothing
Set olApp = Nothing
addContact = getContactFolderCount
End If
Exit Function
errHandler:
ErrPrint "Function addContact() returned with error"
addContact = 0
End Function
Public Function deleteContact(index As Integer) As Boolean
Dim olApp As Object
Dim olNameSpace As Object
Dim olFolder As Object
Dim olContact As Object
On Error Goto errHandler
If Not isOutlookInstalled Then
deleteContact = False
ElseIf getContactFolderCount < index Then
deleteContact = False
Else
Set olApp = CreateObject("Outlook.Application")
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderContacts)
Set olContact = olFolder.Items(index)
olContact.Delete
Set olContact = Nothing
Set olFolder = Nothing
Set olNameSpace = Nothing
Set olApp = Nothing
deleteContact = True
End If
Exit Function
errHandler:
ErrPrint "Function deleteContact() returned with error"
deleteContact = False
End Function
Public Function findContact(LastName As String, _
Optional FirstName As String = vbNullString) As Integer
Dim olApp As Object
Dim olNameSpace As Object
Dim olFolder As Outlook.MAPIFolder Dim olContact As Outlook.ContactItem Dim i As Integer
On Error Goto errHandler
If Not isOutlookInstalled Then
findContact = 0
Else
Set olApp = CreateObject("Outlook.Application")
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderContacts)
findContact = 0
For i = 1 To olFolder.Items.Count
Set olContact = olFolder.Items(i)
If LCase(olContact.LastName) = LCase(LastName) Then
If FirstName = "" Then
findContact = i
Exit For
ElseIf olContact.FirstName = FirstName Then
findContact = i
Exit For
End If
End If
Next i
Set olContact = Nothing
Set olFolder = Nothing
Set olNameSpace = Nothing
Set olApp = Nothing
End If
Exit Function
errHandler:
ErrPrint "Function findContact() returned with error"
findContact = 0
End Function
Public Sub ErrPrint(str As Variant)
Dim fnr As Long
If doErrorLogging Then
fnr = FreeFile
Open checkPath(App.Path) & errFile For Append As #fnr
Print #fnr, CStr(Time) & vbTab & CStr(str)
Print #fnr, vbTab & "Fehler " & _
CStr(Err.Number) & ": " & Err.Description
Print #fnr, ""
Close #fnr
DoEvents
End If
End SubListing 1: Kontakte mit Outlook auslesen |