I am new to VBA and I need help doing something:
As seen in the picture below, I have a list of names. And what I want to do is retrieve their email address, based o
This code assumes names are in column A. It further assumes that the name of the address book you are tapping into is named "Contacts", and that they are formatted according to your diagram.
Option Explicit
Private Sub GetAddresses()
Dim o, AddressList, AddressEntry
Dim c As Range, r As Range, AddressName As String
Set o = CreateObject("Outlook.Application")
Set AddressList = o.Session.AddressLists("Contacts")
'Change this range accordingly
Set r = Range("A1:A25")
For Each c In r
AddressName = c.Value
For Each AddressEntry In AddressList.AddressEntries
If AddressEntry.Name = AddressName Then
c.Offset(0, 1).Value = AddressEntry.Address
Exit For
End If
Next AddressEntry
Next c
End Sub
If the addresses are in the Global Address List, In Outlook, go to Tools--> Address Book. Then use the drop-down list to identify which list your addresses are in. Replace "Contacts" in the code with the name of the address book the addresses are stored in.
I didn't write this, I found it on Ozgrid and modified a couple of things to fit your situation. It may take a little tweaking for your application. Hope this helps or gets you going in the right direction.