Solved

Serching The Outlook Address Book Through VB

Posted on 2004-10-01
11
901 Views
Last Modified: 2012-06-21
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
Comment
Question by:Madmarlin
11 Comments
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12201302
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
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 12202143
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
 
LVL 76

Expert Comment

by:David Lee
ID: 12202276
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
 
LVL 1

Author Comment

by:Madmarlin
ID: 12215330
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
 
LVL 76

Accepted Solution

by:
David Lee earned 125 total points
ID: 12222563
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:Madmarlin
ID: 12247297


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
 
LVL 76

Expert Comment

by:David Lee
ID: 12247394
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
 
LVL 1

Author Comment

by:Madmarlin
ID: 12247447

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
 
LVL 76

Expert Comment

by:David Lee
ID: 12247573
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
 
LVL 1

Author Comment

by:Madmarlin
ID: 12380549
Message for BlueDevilFan...

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

Cheers Chris
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12396284
Chris,

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

-- BDF
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now