'Option Explicit
Sub Get_OutlookContacts_New()
' "VBA : Extract contact list from Outlook ("Global Address List")"
' To populate a list of all outlook contacts and their property details
' Working Fine...
' 1) Need to automatically assign the exact Item number of the AddressList for "@yahoo.com" email account
' 2) Need to speed up the processing
Dim objOutlook As Outlook.Application
Dim objAddressBook As Outlook.AddressList
Dim objAddressEntry As Outlook.AddressEntry
Dim lngRow As Long
Dim OutSess As Outlook.Account
Dim iCnt As Long
Dim iTtlGAL As Long, iCntGAL As Long
With Application
.ScreenUpdating = False: .DisplayAlerts = False
.EnableEvents = False: .Calculation = xlCalculationManual: End With
Set objOutlook = CreateObject("Outlook.Application")
For iCnt = 1 To objOutlook.Session.Accounts.Count
If MsgBox("Your e-mail account name: " & vbTab & objOutlook.Session.Accounts.Item(iCnt) & vbCrLf & _
"This is account number: " & vbTab & iCnt & " out of " & objOutlook.Session.Accounts.Count & vbCrLf & _
vbCrLf & _
"Would you like to proceed with this account?", vbYesNo + vbQuestion) = vbYes Then
Set OutSess = objOutlook.Session.Accounts(iCnt)
With OutSess.Session
For iLoop = 1 To .AddressLists.Count
If .AddressLists.Item(iLoop) = "Global Address List" Then
iTtlGAL = iTtlGAL + 1
End If
Next iLoop
For iLoop = 1 To .AddressLists.Count
If .AddressLists.Item(iLoop) = "Global Address List" Then
iCntGAL = iCntGAL + 1
If MsgBox("Item number: " & iLoop & " out of " & .AddressLists.Count & vbNewLine & _
"Item name: " & .AddressLists.Item(iLoop) & vbNewLine & _
"Total avaliable Address Entries: " & .AddressLists.Item(iLoop).AddressEntries.Count, vbYesNo + vbQuestion, "Note: This GAL number is " & iCntGAL & " out of " & iTtlGAL) = vbYes Then
Set myAddressList = OutSess.Session.AddressLists.Item(iLoop) '("Global Address List") for the second 'GAL'
iYes = 1
GoTo iNxtAcctLine:
End If
End If
Next iLoop
End With
End If
Next iCnt
iNxtAcctLine:
If iYes <> 1 Then: Exit Sub:
MsgBox "Now working with e-mail address: " & vbTab & OutSess & vbNewLine & _
"Current Users Count: " & vbTab & vbTab & myAddressList.AddressEntries.Count, vbInformation, OutSess
Dim iPutRecrdOn As Long
Dim aaa As Variant
'Dim A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z As String
With OutSess
Sheets("Sheet1").Cells.Clear ' Clear existing list
On Error Resume Next
Application.ScreenUpdating = False
'Step through each contact and list each that has an email address
For Each objAddressEntry In myAddressList.AddressEntries
Application.ScreenUpdating = False
If objAddressEntry.Address <> "" Then
intCounter = intCounter + 1
iPutRecrdOn = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1
Application.StatusBar = "Address no. " & intCounter & ".../ " & myAddressList.AddressEntries.Count & ": " & Format(((intCounter / myAddressList.AddressEntries.Count) * 100), "0.00") & "% " & objAddressEntry.name 'objAddressEntry.Address
For Each aaa In Array("@", "~") ' ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z") ' , "1", "2", "3", "4", "5", "6", "7", "8", "9"
If aaa <> (UCase(Left(Trim(objAddressEntry.name), 1))) Then
Sheets("Sheet1").Cells(iPutRecrdOn, 1) = objAddressEntry.name
Sheets("Sheet1").Cells(iPutRecrdOn, 2) = objAddressEntry.GetExchangeUser.Alias
Sheets("Sheet1").Cells(iPutRecrdOn, 3) = objAddressEntry.GetExchangeUser.PrimarySmtpAddress
Sheets("Sheet1").Cells(iPutRecrdOn, 4) = objAddressEntry.GetExchangeUser.GetExchangeUserManager.LastName & ", " & objAddressEntry.GetExchangeUser.GetExchangeUserManager.FirstName
Sheets("Sheet1").Cells(iPutRecrdOn, 5) = objAddressEntry.GetExchangeUser.GetExchangeUserManager.Alias
' Sheets("Sheet1").Cells(iPutRecrdOn, 6) = objAddressEntry.GetExchangeUser.Address
' DoEvents
Exit For:
End If
Next aaa
End If
Next objAddressEntry
On Error GoTo 0
Application.StatusBar = False
End With
'****************************************************************************************************
Set objOutlook = Nothing
Set myAddressList = Nothing
MsgBox "Done!", vbInformation, ThisWorkbook.name
With Application
.ScreenUpdating = False: .DisplayAlerts = False
.EnableEvents = False: .Calculation = xlCalculationAutomatic: End With
End Sub
3)
Bookmarks