Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   Code Library (http://www.xtremevbtalk.com/code-library/)
-   -   Accessing the Outlook Contact List (http://www.xtremevbtalk.com/code-library/160009-accessing-outlook-contact-list.html)

reboot 04-11-2004 02:16 PM

Accessing the Outlook Contact List
 
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

Code:
outlookContacts = New GetOutlookContacts Textbox1.Text = outlookContacts.GetFirstContact() outlookContacts = Nothing
and the Class

Code:
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.olFolderContacts) _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


All times are GMT -6. The time now is 09:37 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.