How To Resolve Ambiguous Names with the Outlook Object Model Using Visual Basic (260656)



The information in this article applies to:

  • Microsoft Outlook 2000
  • Microsoft Outlook 98
  • Microsoft Visual Basic Professional Edition for Windows 6.0

This article was previously published under Q260656

SUMMARY

This article demonstrates how to programmatically handle duplicate names that cause ambiguous name resolution when you use the Outlook Object Model (OOM).

MORE INFORMATION

To resolve duplicate names programmatically, you must have another method of identifying each recipient (other than the recipient's name).

The following sample subroutine uses each recipient's e-mail address to confirm the selection of the duplicate recipients. You can also use any other information that you have for each recipient if the data corresponds with the properties that are provided in the OOM.
Sub AmbiguousNameRes()
Dim spOutlook As Outlook.Application
Dim spNameSpace As Outlook.NameSpace
Dim spRecipient As Recipient
Dim spAdrLsts As AddressLists
Dim spAdrLst As AddressList
Dim spAdrEnts As AddressEntries
Dim spAdrEnt As AddressEntry
   ' Ambiguous name resolution technique.
   Set spOutlook = CreateObject("Outlook.Application")
   Set spNameSpace = spOutlook.GetNamespace("MAPI")
   Set spRecipient = spNameSpace.CreateRecipient("Recipient Name")

   spRecipient.Resolve

   If spRecipient.Resolved = False Then ' Failed due to 0 or >1 matches.
      Set spAdrLsts = spNameSpace.AddressLists
      For Each spAdrLst In spAdrLsts
         Set spAdrEnts = spAdrLst.AddressEntries
         For Each spAdrEnt In spAdrEnts
            If spAdrEnt.Name = spRecipient.Name Then
               ' Determine if correct name.
              If spAdrEnt.Address = "Recipient@EmailAddress" Then
                 Set spRecipient.AddressEntry = spAdrEnt
                 spRecipient.Resolve
                 If spRecipient.Resolved = True Then
                    ' You have a valid recipient object.
                    ' Send mail using this recipient.
                 Else
                   ' Try other properties to identify correct one?
                 End If
               End If
            End If
         Next
      Next
      Set spAdrEnts = Nothing
   End If
   Set spAdrLsts = Nothing
   Set spNameSpace = Nothing
   Set spOutlook = Nothing
End Sub
				

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto kbMsg kbOutlookObj KB260656