Accessing the Outlook Contact List

reboot
04-11-2004, 02:16 PM
Not extremely well commented, but I think the code is pretty simple and self-explanatory. Also not intended to be any kind of complete app, but just a "here's how it's done" sample. In real life, you'd probably want Properties for all the individual Contact items, instead of returning a big string with all of them in it like I did here. There are also other methods I didn't make use of for this sample. Find and FindNext, for example, would probably be pretty useful to include in the class. I always like to leave something for the reader to play with. :)

To use this, you'll need to include the Outlook COM Library Reference (when will MS ever bring Office into the .Net fold?? :-\).

AND.... No "reboot doing VBA??" cracks either! :p

Sample usage would be something like

outlookContacts = New GetOutlookContacts

Textbox1.Text = outlookContacts.GetFirstContact()

outlookContacts = Nothingand the Class

Imports System
Imports System.Text
Imports System.Environment

'walks the outlook contact list
Public Class GetOutlookContacts
Private _olApplication As Outlook.Application
Private _olNameSpace As Outlook.NameSpace
Private _olContacts As Outlook.MAPIFolder
Private _olItems As Outlook.Items
Private _olContactItem As Outlook.ContactItem

'constructor
Public Sub New()
'create Outlook application.
_olApplication = New Outlook.Application

'get nameSpace and logon.
_olNameSpace = _olApplication.GetNamespace("mapi")
_olNameSpace.Logon("Outlook", "", False, True)

'get the contact items
_olContacts = _olNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContact s)
_olItems = _olContacts.Items
End Sub

Public Function GetFirstContact() As String
'get the first contact from the Contacts folder.
Try
_olContactItem = CType(_olItems.GetFirst(), Outlook.ContactItem)

Return BuildContactString()
Catch
Return "An error occurred"
End Try
End Function

Public Function GetPreviousContact() As String
'get the previous contact from the Contacts folder.
Try
_olContactItem = CType(_olItems.GetPrevious(), Outlook.ContactItem)

Return BuildContactString()
Catch
Return "An error occurred"
End Try
End Function

Public Function GetNextContact() As String
'get the next contact from the Contacts folder.
Try
_olContactItem = CType(_olItems.GetNext(), Outlook.ContactItem)

Return BuildContactString()
Catch
Return "An error occurred"
End Try
End Function

Public Function GetLastContact() As String
' Get the last contact from the Contacts folder.
Try
_olContactItem = CType(_olItems.GetLast(), Outlook.ContactItem)

Return BuildContactString()
Catch
Return "An error occurred"
End Try
End Function

Private Function BuildContactString() As String
Dim sb As New StringBuilder

'return some common properties.
sb.Append(_olContactItem.FullName)
sb.Append(NewLine)
sb.Append(_olContactItem.Title)
sb.Append(NewLine)
sb.Append(_olContactItem.Birthday)
sb.Append(NewLine)
sb.Append(_olContactItem.CompanyName)
sb.Append(NewLine)
sb.Append(_olContactItem.Department)
sb.Append(NewLine)
sb.Append(_olContactItem.Body)
sb.Append(NewLine)
sb.Append(_olContactItem.FileAs)
sb.Append(NewLine)
sb.Append(_olContactItem.Email1Address)
sb.Append(NewLine)
sb.Append(_olContactItem.BusinessHomePage)
sb.Append(NewLine)
sb.Append(_olContactItem.MailingAddress)
sb.Append(NewLine)
sb.Append(_olContactItem.BusinessAddress)
sb.Append(NewLine)
sb.Append(_olContactItem.OfficeLocation)
sb.Append(NewLine)
sb.Append(_olContactItem.Subject)
sb.Append(NewLine)
sb.Append(_olContactItem.JobTitle)

Return sb.ToString
End Function

'destructor
Protected Overrides Sub Finalize()
'log off.
_olNameSpace.Logoff()

'clean up.
_olApplication = Nothing
_olNameSpace = Nothing
_olItems = Nothing
_olContactItem = Nothing
End Sub
End Class

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum