Solved

Serching The Outlook Address Book Through VB

Posted on 2004-10-01
11
906 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 52

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

751 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