Look up e-mail recipients from Outlook address book

Here is what I need to be able to do.
We use Outlook 98 across the board around here.  If a user is composing an e-mail message and the recipient is something like 'Joe Blow', they can usually just write "Blow, J" or something like that and Outlook will resolve the rest.  It looks up the contact in the global address book and if there is only one match, it replaces "Blow, J" with that contact.  If there is more than one match it prompts the user to pick one.

In my app users can click a button to create a new e-mail message, with the body populated with info from the current record.  I have built in something that will also populate the 'To' field with a list of contacts.  This list is maintained by the users on a diferent form.  Each contact is just a name in plain text and optionally an e-mail address, but in most cases this is not required since it is always an internal contact.  What I do is create a new MAPI.Recipient, assign the e-mail address if applicable, assign its name accordingly, and then call MAPI.Recipient.Resolve to have it resolve to a contact on the outlook message.

What I would like to do is have the resolution happen on the other form where the list is maintained.  i.e., a user enters all or part of a contact name into my text box, clicks OK, and then it would resolve to the correct contact.  I would need to store the full name and e-mail address.  There would be no change to the way my e-mails are sent, it would still just have to take the plain text name and e-mail and assign it to a MAPI.Recipient.  The difference is that the user is able to check if the contact is valid or not when they first save that text.

In other words, I have a (full or partial) name, and I want a proper contact name and e-mail address from the global address book.

I hope this makes sense.  How can it be accomplished?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pteehanAuthor Commented:
Well, that's a good start...but it doesn't really do what I need, which is basically a function to look up a name query.  Partial name goes in, full name and e-mail address come out.  And sadly I have no time to figure it out for myself, hence the 500 points :)

Also I am hesitant to get into specific application code in case the client changes (at the very least to a new version of Outlook, which might happen soon).  The code I have in place now uses the MAPI interface and ideally I could use that if possible (though if there is no other option we could at least look at it).

I would think the MAPI.Recipient.Resolve function must do what I need somehow.  I just don't know how to use it properly.  

Perhaps this is what you want. It is a small form that resolves a single name against the Outlook address book. You should be able to something similar on your second form, looping through the names, and save the information as you need.

In a new project add a referenct to Microsoft Outlook Object Library
On the form, add a text box and a command button.
Paste this code into the form.

Private Sub Command1_Click()
    Dim OutlookApp As Outlook.Application
    Dim MailItem As Outlook.MailItem
    Dim Recipient As Outlook.Recipient
    Set OutlookApp = New Outlook.Application
    Set MailItem = OutlookApp.CreateItem(0)
    Set Recipient = MailItem.Recipients.Add(Text1.Text)
    If Recipient.Resolve Then
        Text1.Text = Recipient.Name
        MsgBox "There is an invalid recipient"
    End If
    Set Recipient = Nothing
    Set MailItem = Nothing
    Set OutlookApp = Nothing
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

pteehanAuthor Commented:
Great!  Thanks.  Getting very close now.  I could probably use this.

THe only problem is that it is likely to fail a lot (there are a LOT of people at my company and thus a lot of ambiguous names) and there isn't really much they can do.  For example, if they enter John Smith it will always fail, unless I can get them to look up the e-mail address, but that will make it more difficult to use.

When you try and send a message to an ambiguous user in Outlook it pops up a dialog box with a list of possible matches, and it resolves to the one you pick.   Is it possible to launch this dialog if resolve fails and catch the name that the user picks?

I'm having trouble finding something useful to put in the else for a failed resolve. I'll keep looking though!


pteehanAuthor Commented:
I did some research into the CDO object reference on MSDN and was able to find an example that I could adapt.  I sidestepped the problem of failing resolves by focing them to choose a name from the original address book.  Here is the code for anyone who happens upon this page:

Function Session_AddressBook() As MAPI.Recipient
    On Error GoTo err_Session_AddressBook

    If objSession Is Nothing Then
        'Session_AddressBook = "LOOKUP FAILED"
       ' MsgBox "Must first create MAPI session and logon"
        Exit Function
    End If
    Set objRecipColl = objSession.AddressBook( _
                       Title:="Select Contact", _
                       oneAddress:=True, _
                       forceResolution:=True, _
                       recipLists:=1, _
                       toLabel:="&Recipient") ' appears on button
    ' Note: first parameter ("recipients") not used in this call
    ' recipients:=objInitRecipColl initializes recipients for dialog
'    MsgBox "Name of first recipient = " & objRecipColl.Item(1).Name
    Set Session_AddressBook = objRecipColl.item(1)
    Exit Function

    If (Err = 91) Then ' MAPI dlg-related function that sets an object
        'MsgBox "No recipients selected"
       ' MsgBox "Unrecoverable Error:" & Err
    End If
    Exit Function
End Function

Dang I appreciate your help even though I eventually found the answer myself.  I do want to reward your efforts but I am reluctant to let go of my 500 points! :)  What should I do.
    Nice solution, glad you got your problem worked through. Sorry I couldn't provide more help, something with a higher priority came in a few days later from my boss; you know how that goes.

BTW: You could drop the point value to what ever you feel is appropriate.


    Thanks!   : )
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.