I have an excel sheet where users need to enter email addresses...is there any way to have their Outlook address book come up in Excel so the can simply select the addresses from there and have the selected addresses appear in the Excel sheet cell?
Private Sub GetOutlookAddressBook()
' Need to add reference to Outlook
'(In VBA editor Tools References MS Outlook #.# Library)
' Adds addresses to existing Sheet called Address and
' defines name Addresses containing this list
' For use with data Validation ListBox (Source as =Addresses)
On Error GoTo error
Dim objOutlook As Outlook.Application
Dim objAddressList As Outlook.AddressList
Dim objAddressEntry As Outlook.AddressEntry
Dim intCounter As Integer
' Setup connection to Outlook application
Set objOutlook = CreateObject("Outlook.Application")
Set objAddressList = objOutlook.Session.AddressLists("Contacts")
Application.EnableEvents = False
' Clear existing list
Sheets("Address").Range("A:A").Clear
'Step through each contact and list each that has an email address
For Each objAddressEntry In objAddressList.AddressEntries
If objAddressEntry.Address <> "" Then
intCounter = intCounter + 1
Application.StatusBar = "Address no. " & intCounter & " ... " & objAddressEntry.Address
Sheets("Address").Cells(intCounter, 1) = objAddressEntry.Address
DoEvents
End If
Next objAddressEntry
' Define range called "Addresses" to the list of emails
Sheets("Address").Cells(1, 1).Resize(intCounter, 1).Name = "Addresses"
error:
Set objOutlook = Nothing
Application.StatusBar = False
Application.EnableEvents = False
End Sub
Global Address List
Enterprise
Messaging1
Recipients
Message2
Recipients
-addresses-
Dim objOutlook As Outlook.Application
Dim objAddressList As Outlook.AddressList
Dim objAddressEntry As Outlook.AddressEntry
Dim intCounter As Integer
Dim counter As Integer
Dim addrCount As Integer
' Setup connection to Outlook application
Set objOutlook = CreateObject("Outlook.Application")
Set objAddressList = objOutlook.Session.AddressLists("Global Address List")
'number of address lists in Global Address List
addrCount = objOutlook.Session.AddressLists.Count
MsgBox ("addrCount = " & addrCount)
'print out the name of each address list in column
For Each objAddressList In objOutlook.Session.AddressLists
counter = counter + 1
Sheets("Address").Cells(counter, 2) = objAddressList.Name
Next objAddressList
Set objAddressList = objOutlook.Session.AddressLists(526)
For Each objAddressEntry In objAddressList.AddressEntries
intCounter = intCounter + 1
Sheets("Address").Cells(intCounter, 1) = objAddressEntry.Name
DoEvents
Next objAddressEntry
For Each objAddressList In objOutlook.Session.AddressLists
If objAddressList.ID = "00000000DCA740C8C042101AB4B908002B2FE18201000000000100002F6F3D456E7465 7270726973652F6F753D4B4E412043616E616461204D6573736167696E672F636E3D45 78557365727300" Then
For Each objAddressEntry In objAddressList.AddressEntries
intCounter = intCounter + 1
Sheets("Test" ).Cells(intCounter, 1) = objAddressEntry.Name
DoEvents
Next
End If
Next objAddressList
On Error Resume Next
If GetObject(, "Outlook.Application") Is Nothing Then
'Outlook is not running
End If
[FONT=Verdana][COLOR=black][COLOR=black][FONT=Verdana]Dim Outlook As Object[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]Const olFolderContacts As Long = 10[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]Private Sub Worksheet_Change(ByVal Target As Range)[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]Dim contactName As String[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]Dim contacts As Object[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Dim contact As Object[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Dim comment As Excel.comment[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Dim contactInfo As String[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Dim addressLists As Object ' Outlook.AddressLists[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Dim GAL As Object ' Outlook.AddressList[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Dim addressEntries As Object ' Outlook.AddressEntries[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Dim addressEntry As Object ' Outlook.AddressEntry[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]' get target cell value ONLY if single cell selected[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]If Target.Cells.Count = 1 Then[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]contactName = Target.Value[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Else[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Exit Sub[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]End If[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]' ignore blanks[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]If Len(contactName) = 0 Then[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Exit Sub[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]End If[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]' grab Outlook, if not already instantiated previously[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]If Outlook Is Nothing Then[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Set Outlook = GetOutlookApp[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]End If[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]' get contacts[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]Set contacts = GetItems(GetNS(Outlook), olFolderContacts)[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]' try to grab target contact[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]On Error Resume Next[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Set contact = contacts.Item(contactName)[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]On Error GoTo 0[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]' remove existing comment, if any[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]On Error Resume Next[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Set comment = Target.comment[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]comment.Delete[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]On Error GoTo 0[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]If contact Is Nothing Then[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]' try to find in GAL[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]Set addressLists = GetNS(Outlook).addressLists[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Set GAL = addressLists.Item("Global Address List")[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Set addressEntries = GAL.addressEntries[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]On Error Resume Next[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Set addressEntry = addressEntries.Item(Target.Value)[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]On Error GoTo 0[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]If addressEntry Is Nothing Then[/FONT][/COLOR]
[FONT=Verdana][COLOR=black] ' nothing in Contacts Folder or GAL[/COLOR][/FONT]
[FONT=Verdana][COLOR=black] contactInfo = "No contact found with this name."[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Else[/COLOR][/FONT]
[FONT=Verdana][COLOR=black] ' in GAL but not Contacts Folder[/COLOR][/FONT]
[FONT=Verdana][COLOR=black] contactInfo = addressEntry.Name & Chr(10) & addressEntry.Address & Chr(10) & _[/COLOR][/FONT]
[FONT=Verdana][COLOR=black] Chr(10) & "This information came from the Global Address List."[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]End If[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Else[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]' in Contacts Folder[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]contactInfo = contact.Email1Address[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]End If[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]' put contact info into adjacent cell[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]' turn off Events to avoid event firing again[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Application.EnableEvents = False[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Target.Offset(0, 1).Value = contactInfo[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Application.EnableEvents = True[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]End Sub[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Function GetOutlookApp() As Object[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Set GetOutlookApp = CreateObject("Outlook.Application")[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]End Function[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Function GetNS(ByRef app As Object) As Object[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Set GetNS = app.GetNamespace("MAPI")[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]End Function[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Function GetItems(olNS As Object, folder As Long) As Object[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]Set GetItems = olNS.GetDefaultFolder(folder).Items[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]End Function[/COLOR][/FONT]
[/COLOR][/FONT]