Excel-VBA gets filtered collection from Outlook AddressList

Problem: It seems to me that AddressList does not have built-in filter functions, for example, C # DataTable (DatTableObject.Select (filter criteria), I'm looking for a way to do this.

The global address list I am referring to has about a million entries, and I need to search it up to 1000 times.

I use the name of the exchange user to find emails of people using the following code:

Set olApp = CreateObject("Outlook.Application") Set myNamespace = olApp.GetNamespace("MAPI") Set aList = myNamespace.AddressLists.Item("Global Address List") Set aEntry = aList.AddressEntries("" + ExchangeName + "") Set exUser = aEntry.GetExchangeUser 

But it only returns me one AddressEntry, which is a problem when I have several people with the same Exchange name. This happens quite often.

Question: When I look at the global address list in Outlook, I have everything sorted alphabetically and with good speed, I am presented with all matches, starting from the line in which I print. How can I get a similar collection in VBA?

+4
source share
1 answer

AddressEntries object is a collection of AddressEntry objects.
When you index directly into an AddressEntries collection like you, you return a single AddressEntry object based on the provided Index parameter. The Index parameter can be either an index number, or it can be a standard property of an element.

Since the default property of the .Name element is a .Name property, a return is that the first element in the collection matches the .Name property.


If you want to return all AddressEntry objects in the collection that match the .Name property, you will need to loop through the collection.

Now, in .Net, you can iterate over a collection using For...Next , and I believe that you can do it in VBA too, but I just can't remember it. eg:

 Set olApp = CreateObject("Outlook.Application") Set myNamespace = olApp.GetNamespace("MAPI") Set aList = myNamespace.AddressLists.Item("Global Address List") Set aEntries = aList.AddressEntries For each aEntry in aEntries if aEntry.Name="" + ExchangeName + "" Then 'Do something with aEntry object End If Next 

If for some reason this does not work, you can iterate through the collection using the GetFirst and GetNext .
eg:.

 Set olApp = CreateObject("Outlook.Application") Set myNamespace = olApp.GetNamespace("MAPI") Set aList = myNamespace.AddressLists.Item("Global Address List") Set aEntries = aList.AddressEntries Set aEntry = aEntries.GetFirst Do While Not aEntry is Nothing if aEntry.Name="" + ExchangeName + "" Then 'Do something with aEntry object End If Set aEntry = aEntries.GetNext Loop 

For sorting in alphabetical order, see Sort Method .


And as for creating functionality, where

... I have been presented with all matches starting with the line that I enter.

I'm not sure how you want to "represent" matches or where you want to enter a string. But the general idea would be to create a function that takes an input parameter (for example, "MatchName" As String ) and executes a loop like above to find all the matches in this string for any property that you want to see, and then you will return an array, or something that you could use to "represent" the information.

If you want to make it dynamic so that the updated list is "as you type", you can start the update procedure from the KeyPress event. In order not to run the entire collection when you enter a word, you probably want to save the array, and then with each letter you add, you can simply iterate over this array and remove inconsistencies (narrow down the results). Before this happens, you probably need some kind of check to check if the letter has been deleted (for example, checking the length of the string in the text field), which will tell your program to re-check the AddressEntries collection AddressEntries expand the results).

In any case, such a general idea in one way you could do it.

+1
source

All Articles