Look up e-mail recipients from Outlook address book

Posted on 2003-11-19
Last Modified: 2007-12-19
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?


Question by:pteehan
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
  • 5
  • 3

Expert Comment

ID: 9782688

Author Comment

ID: 9782800
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.  


Accepted Solution

Dang123 earned 250 total points
ID: 9787360
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Author Comment

ID: 9789378
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?


Expert Comment

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



Author Comment

ID: 9850465
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.

Expert Comment

ID: 9850729
    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.



Expert Comment

ID: 9857690
    Thanks!   : )

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

631 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