Hi,
Looking for a way to quickly find e-mail addresses associated with a contact's name I wrote a program to do exactly this and it works fine except for the fact that when dealing with an addressbook containing 70000 or so contacts it's so slow that it takes several minutes to read the addressbook or to go through all the contacts collected to find an e-mail address.
When the user selects an addressbook or an address entry type (Outlook.OlAddressEntryUserType) it collects all contacts that meet the criteria using the following code:
"e.Result" refers to return value for the backgroundworker object used to execute the above code.Code:e.Result = (From Contact In .OutlookO.GetNamespace("MAPI").AddressLists(.AddressList).AddressEntries Where DirectCast(Contact, Outlook.AddressEntry).AddressEntryUserType = .AddressType Select DirectCast(Contact, Outlook.AddressEntry)).ToArray()
"ContactsSource.Contacts" refers to an Outlook.AddressEntry array that is part of the public ContactsSource structure.
Does any one here know how to optimize the code so that it only takes seconds instead of minutes to go through a large number contacts? Any help would be appreciated.Code:Dim EMailAddresses As New List(Of String) Dim FirstContact As Outlook.AddressEntry = Nothing Name = Name.Trim() If ContactsSource.Contacts.Count > 0 Then FirstContact = ContactsSource.Contacts.First If FirstContact.GetContact() IsNot Nothing Then For Each Contact As Outlook.AddressEntry In From ContactO In ContactsSource.Contacts Where ContactO.GetContact().FullName.IndexOf(Name, StringComparison.CurrentCultureIgnoreCase) > -1 With Contact.GetContact() EMailAddresses.AddRange({ .Email1Address, .Email2Address, .Email3Address}) End With Next Contact ElseIf FirstContact.GetExchangeDistributionList() IsNot Nothing Then For Each Contact As Outlook.AddressEntry In From ContactO In ContactsSource.Contacts Where ContactO.GetExchangeDistributionList.Name.IndexOf(Name, StringComparison.CurrentCultureIgnoreCase) > -1 EMailAddresses.Add(Contact.GetExchangeDistributionList.PrimarySmtpAddress) Next Contact ElseIf FirstContact.GetExchangeUser() IsNot Nothing Then For Each Contact As Outlook.AddressEntry In From ContactO In ContactsSource.Contacts Where ContactO.GetExchangeUser.Name.IndexOf(Name, StringComparison.CurrentCultureIgnoreCase) > -1 EMailAddresses.Add(Contact.GetExchangeUser.PrimarySmtpAddress) Next Contact End If
The full program code has been attached to this post as Sea.zip.
Thanks in advance.




Reply With Quote
