I am trying to load the Outlook Global Address List into a table in a db. I thought I had it, but its missing people.
If I go into Outlook I can find the person....but my code is not and i can't figure out why.
I found the code and have adjusted it to write to a table.
Public Sub Get_EM2()
'--------This function pulls names and email addresses from the global address book -------------------------
Dim MyObj As New Outlook.Application
Dim NameSpace As Outlook.NameSpace
Dim GAL As AddressList, sName As String, allGAL As AddressEntries
Dim db As Database
Dim rs As Recordset
Dim i As Long
Dim entry As AddressEntry
Dim exUser As ExchangeUser
Dim strFirstName$, strLastName$, strBusPhone$, strMobilePhone$, strEmail1$
Set NameSpace = MyObj.GetNamespace("MAPI")
Set GAL = NameSpace.AddressLists("Global Address List")
Set allGAL = GAL.AddressEntries
GAL.AddressEntries.Sort
Set db = CurrentDb
Set rs = db.OpenRecordset("empTable")
For i = 1 To GAL.AddressEntries.Count - 1
'.List1.AddItem GAL.AddressEntries.Item(i).Name
Set entry = allGAL.Item(i)
If entry.AddressEntryUserType = olExchangeUserAddressEntry Then
Set exUser = entry.GetExchangeUser
' check for blank last name
If exUser.LastName <> "" Then
' Get field values
strFirstName$ = exUser.FirstName
strLastName$ = exUser.LastName
strBusPhone$ = exUser.BusinessTelephoneNumber
strMobilePhone$ = exUser.MobileTelephoneNumber
strEmail1$ = exUser.PrimarySmtpAddress
rs.AddNew
' rs!FirstName = GAL.AddressEntries.Item(i).Name
rs!FirstName = strFirstName$
rs!LastName = strLastName$
rs!emailAddress = strEmail1$
rs!BusinessTelNum = strBusPhone$
rs.Update
Debug.Print "Working On: " & i
End If
End If
Next i
Debug.Print "Successful Completion..."
rs.Close
Set NameSpace = Nothing
Set GAL = Nothing
Set db = Nothing
Set rs = Nothing
End Sub
If I go into Outlook I can find the person....but my code is not and i can't figure out why.
I found the code and have adjusted it to write to a table.
Public Sub Get_EM2()
'--------This function pulls names and email addresses from the global address book -------------------------
Dim MyObj As New Outlook.Application
Dim NameSpace As Outlook.NameSpace
Dim GAL As AddressList, sName As String, allGAL As AddressEntries
Dim db As Database
Dim rs As Recordset
Dim i As Long
Dim entry As AddressEntry
Dim exUser As ExchangeUser
Dim strFirstName$, strLastName$, strBusPhone$, strMobilePhone$, strEmail1$
Set NameSpace = MyObj.GetNamespace("MAPI")
Set GAL = NameSpace.AddressLists("Global Address List")
Set allGAL = GAL.AddressEntries
GAL.AddressEntries.Sort
Set db = CurrentDb
Set rs = db.OpenRecordset("empTable")
For i = 1 To GAL.AddressEntries.Count - 1
'.List1.AddItem GAL.AddressEntries.Item(i).Name
Set entry = allGAL.Item(i)
If entry.AddressEntryUserType = olExchangeUserAddressEntry Then
Set exUser = entry.GetExchangeUser
' check for blank last name
If exUser.LastName <> "" Then
' Get field values
strFirstName$ = exUser.FirstName
strLastName$ = exUser.LastName
strBusPhone$ = exUser.BusinessTelephoneNumber
strMobilePhone$ = exUser.MobileTelephoneNumber
strEmail1$ = exUser.PrimarySmtpAddress
rs.AddNew
' rs!FirstName = GAL.AddressEntries.Item(i).Name
rs!FirstName = strFirstName$
rs!LastName = strLastName$
rs!emailAddress = strEmail1$
rs!BusinessTelNum = strBusPhone$
rs.Update
Debug.Print "Working On: " & i
End If
End If
Next i
Debug.Print "Successful Completion..."
rs.Close
Set NameSpace = Nothing
Set GAL = Nothing
Set db = Nothing
Set rs = Nothing
End Sub