• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 917
  • Last Modified:

Serching The Outlook Address Book Through VB

Hello experts,

Im working on a project which involves automating an email.

The users address book is very large and therefore can really wait for the full address book to load.

Is there a way of searching the addressbook content by certain criteria??

My code is as follows (currently loads all addressbook enteries, but say I wanted to list all enteries with the word 'Chris' in the name field)

Private Sub Command1_Click()
Dim oRecipiant As ListItem
Dim myAddressList As AddressList
Dim AddressEntry As AddressEntry

'Clear Address Book
lvAddressBook.ListItems.Clear
lvAddressBook.ColumnHeaders.Clear
lvAddressBook.View = lvwReport

'Add Columns
With lvAddressBook.ColumnHeaders
    .Add , , "Name"
    '.Add , , "Other Field"
End With

Set myAddressList = Application.Session.AddressLists("Global Address List")

For Each AddressEntry In myAddressList.AddressEntries
    Set oRecipiant = lvAddressBook.ListItems.Add(, , AddressEntry.Name)
Next

End Sub
0
Madmarlin
Asked:
Madmarlin
1 Solution
 
fds_fatboyCommented:
Do you mean something like this?

Dim sSearchName As String
sSearchName = "*chris*"
...

For Each AddressEntry In myAddressList.AddressEntries
    If Lcase$(AddressEntry.Name) like sSearchName then
        Set oRecipiant = lvAddressBook.ListItems.Add(, , AddressEntry.Name)
    End If
Next

...
0
 
Ryan ChongCommented:
or try use Instr function, like:

Private Sub Command1_Click()
Dim oRecipiant As ListItem
Dim myAddressList As AddressList
Dim AddressEntry As AddressEntry

'Clear Address Book
lvAddressBook.ListItems.Clear
lvAddressBook.ColumnHeaders.Clear
lvAddressBook.View = lvwReport

'Add Columns
With lvAddressBook.ColumnHeaders
    .Add , , "Name"
    '.Add , , "Other Field"
End With

Set myAddressList = Application.Session.AddressLists("Global Address List")

For Each AddressEntry In myAddressList.AddressEntries

If Instr(1,lcase$(AddressEntry.Name), lcase$(sSearchName )) > 0 then
    Set oRecipiant = lvAddressBook.ListItems.Add(, , AddressEntry.Name)
End If

Next

End Sub
0
 
David LeeCommented:
In either case the code is going to have to read the entire address book, whether it populates the LV control with all the names or a subset of the names.  If you aren't tied to using the Outlook Object Model though, then you could use CDO which has a filtering capability which'd save having to read the entire address list to find just those names that match.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdo/html/_olemsg_addressentryfilter_object.asp
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
MadmarlinAuthor Commented:
The comments from BlueDevilFan are correct, my problem is the number of enteries in the addresslist is ridiculously large therefore I cant loop through each one indiviually.

I am also trying to run the code the BlueDevilFan sent me without success.
I am running VB6 and the version of the CDO.DLL on my platform is 1.21 and not 1.1 so im not sure if somthing has changed, and cant seem to find any refernce to a change anywhere.

SO if proven source could be provided I am prepared to up the points.

Cheers Madmarlin
0
 
David LeeCommented:
Madmarlin,

The code below should ge tthe job done.  I tested it in my Exchange environment and it worked fine.  The CDO on my system is Microsoft CDO 1.21 Library too.  The filter will find all address entries that have the filter name anywhere in them (i.e. first name, last name, hyphenated last name).

    Dim oSession As New MAPI.Session, _
        oThisPAB As AddressList, _
        oAddrEntries As AddressEntries, _
        oAddrEntFilt As AddressEntryFilter, _
        oAddress As AddressEntry, _
        strProfileName As String
    strProfileName = "Outlook"                                                          'Change the profile name as needed
    oSession.Logon strProfileName, , , True
    Set oThisPAB = oSession.AddressLists("Global Address List")          'Change the address list as needed
    Set oAddrEntries = oThisPAB.AddressEntries
    Set oAddrEntFilt = oAddrEntries.Filter
    oAddrEntFilt.Name = "smith"                                                        'Change the name as needed
    For Each oAddress In oAddrEntries
        Debug.Print oAddress                                                              'This is for testing
        'Code to add names to the ListView
    Next
    'oAddrEntFilt.Or = True
    oSession.Logoff
    Set oFolder = Nothing
    Set oSession = Nothing
0
 
MadmarlinAuthor Commented:


Great work BlueDevilFan maybe you can help with this one, its similar and worth 400 points if quick
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21158007.html


Madmarlin
0
 
David LeeCommented:
Hi, Madmarlin.

Thanks.  I saw your other question last night and spent a couple of hours researching the issue.  I can't find anything that reports the quota limits for 5.5.  The best I can offer is code that'll calculate mailbox size for 5.5 (although I can't verify that it works under 5.5 since I don't have access to a 5.5 server anymore).  If you know the quota limts, and if they are consistent (i.e. each user doesn't have a different limit), then you could caluclate the mailbox size, compare it to the limit, and take action as needed based on that comparison.  
0
 
MadmarlinAuthor Commented:

ha ha so i didnt only waste hours myself yesterday I wasted your time aswell, im very sorry.

Yes I do believe that the limit is standard through my user base, so if you know how to check the mailbox size then I uess that would surfice.

On a persoanl note I would just like to say thankyou for you help on both this and the previous problem.

Cheers Madmarlin... ;?)
0
 
David LeeCommented:
No need to be sorry.  I enjoy the challenges I find here in EE.  It keeps the mind sharp.

Ok, I'll post the code for getting the mailbox sizes shortly.  I'll post it to the other question.

You're very welcome.  
0
 
MadmarlinAuthor Commented:
Message for BlueDevilFan...

Did you ever find the code for the other question. (see previosu comments?)

Cheers Chris
0
 
David LeeCommented:
Chris,

I posted the best information I have in the other question.

-- BDF
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now