GetContact returning wrong contact's data

EAhrens

New Member
Joined
Sep 28, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have cobbled together some code designed to allow the user to choose a specific person from their Outlook contacts and return selected info for that contact in cells for use in the workbook. It always returns a different person's info than the one chosen in the GetSelectNamesDialog box. It is somewhat random but typically a close by contact alphabetically. I had co-workers try it and they get the same results. I have tried anything and everything that I could find without any different result. Running Office 365. Thank you.

VBA Code:
Sub OutlookContactSummary()

    Dim outApp As Outlook.Application
    Dim outDialog As SelectNamesDialog
    Dim myAddrList As AddressList
    Dim myAddrEntry As AddressEntry
    Dim AliasName As String
    
    Set outApp = GetObject(, "Outlook.Application")
    Set outDialog = outApp.Session.GetSelectNamesDialog
    Set myAddrList = outApp.GetNamespace("MAPI").AddressLists("Contacts")

    With outDialog
        .AllowMultipleSelection = False
        .InitialAddressList = myAddrList
        .ShowOnlyInitialAddressList = True
        If .Display Then
            AliasName = outDialog.Recipients.Item(1).Name
            Set myAddrEntry = myAddrList.AddressEntries(AliasName)
            Range("F3").Value = myAddrEntry.GetContact.FirstName
            Range("G3").Value = myAddrEntry.GetContact.LastName
            Range("M3").Value = myAddrEntry.GetContact.Email1Address
            Range("H3").Value = myAddrEntry.GetContact.CompanyName
            Range("I3").Value = myAddrEntry.GetContact.BusinessAddressStreet
            Range("J3").Value = myAddrEntry.GetContact.BusinessAddressCity
            Range("K3").Value = myAddrEntry.GetContact.BusinessAddressState
            Range("L3").Value = myAddrEntry.GetContact.BusinessAddressPostalCode
        End If
    End With
    
    Set outApp = Nothing
    Set outDialog = Nothing
    Set myAddrList = Nothing
    Set myAddrEntry = Nothing

End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Instead of this:
AliasName = outDialog.Recipients.Item(1).Name
Set myAddrEntry = myAddrList.AddressEntries(AliasName)

Try GetAddressEntryFromID function with the selected item's EntryID.

VBA Code:
Set myAddrEntry = outApp.Session.GetAddressEntryFromID(outDialog.Recipients.Item(1).EntryID)
 
Upvote 0
Solution
Instead of this:


Try GetAddressEntryFromID function with the selected item's EntryID.

VBA Code:
Set myAddrEntry = outApp.Session.GetAddressEntryFromID(outDialog.Recipients.Item(1).EntryID)
That was the ticket. Works great. Thank you so much! I don't know how many hours I wasted trying to make this work.
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top