Option Compare Database
Option Explicit
Private Sub ExportAccessContacts_Click()
Dim OlApp As Object
Dim olContact As Object
On Error GoTo HandleErr
Const olContactItem = 2
Set OlApp = CreateObject("Outlook.Application")
Dim appOutlook As Outlook.Application
Dim nms As Outlook.Namespace
Dim fld As Outlook.MAPIFolder
Dim itm As Object
Dim con As Outlook.ContactItem
Dim lngContactID As Long
Dim ClickResult As VbMsgBoxResultEx
Dim strFile As String
Dim rst As Dao.Recordset
Set rst = CurrentDb.OpenRecordset("q_Contacts_Search", dbOpenSnapshot)
Set appOutlook = GetObject(, "Outlook.Application")
Set nms = appOutlook.GetNamespace("MAPI")
Set fld = nms.GetDefaultFolder(olFolderContacts)
'Move to the first record in the table/query
rst.MoveFirst
'Continue to do the below loop untill all records are either added to Outlook or have been confirmed that they were already there.
Do Until rst.EOF
lngContactID = rst("Name_ID")
strFile = "C:\Users\" & fOSUserName() & "\Pictures\Contacts\" & rst("First_Name") & " " & rst("Last_Name") & ".png"
Set con = fld.Items.Find("[CustomerID] = " & lngContactID)
Set olContact = OlApp.CreateItem(olContactItem)
If Not TypeName(con) = "Nothing" Then
rst.MoveNext
Else
With olContact
.CustomerID = rst("Name_ID")
.FirstName = Nz(rst("First_Name"), "")
.MiddleName = Nz(rst("MI"), "")
.LastName = rst("Last_Name")
.FullName = rst("FullName")
.FileAs = rst("FullName")
If IsNull(rst("Work_Anniversary")) = False Then
.Anniversary = Format(rst("Work_Anniversary"), "dd/mm/yyyy")
End If
If IsNull(rst("Birthday")) = False Then
.Birthday = Format(rst("Birthday"), "mm/dd/yyyy") 'formatted differently so that it exported the medium date format correctly, because I'm not showing the users the year.
End If
.CompanyName = Nz(DLookup("[Organization]", "[q_Organizations_Search]", "[ORG_Child_ID]=" & Nz(rst("Org_Child_ID"), 0)), "")
.JobTitle = Nz(rst("JobTitle"), "")
.BusinessAddressStreet = Nz(rst("Address"), "")
.BusinessAddressCity = Nz(rst("City"), "")
.BusinessAddressState = Nz(rst("State"), "")
.BusinessAddressCountry = Nz(DLookup("[Country]", "[t_Country]", "[Country_Auto]=" & Nz(rst("Country"), 0)), "")
.BusinessAddressPostalCode = Nz(rst("ZIP_Postal_Code"), "")
.BusinessTelephoneNumber = Nz(Format(rst("Business_Phone"), "(###)###-####"), "")
.MobileTelephoneNumber = Nz(Format(rst("Mobile_Phone"), "(###)###-####"), "")
.Email1Address = Nz(rst("Email_Address"), "")
.Email2Address = Nz(rst("Other_Email"), "")
'Need to remove rich text so <DIV and other fomatting don't come over. Need to figure out how to remove extra enters.
.Body = Nz(PlainText(rst("Notes")), "") & vbCrLf & "Skype: " & Nz(rst("Skype"), "")
'.WebPage = Nz(rst("Web_Page"), "")
.ManagerName = Nz(DLookup("[FullName]", "[q_Contacts_Search]", "[Name_ID]=" & Nz(rst("Manager_Name_ID"), 0)), "")
.AssistantName = Nz(DLookup("[FullName]", "[q_Contacts_Search]", "[Name_ID]=" & Nz(rst("Assistant_Name_ID"), 0)), "")
If Dir(strFile) <> "" Then
.AddPicture (strFile)
End If
.Save
End With
'Track that the button was clicked.
Dim Action As String
Action = "Mass uploaded to Outlook"
CurrentDb.Execute "INSERT INTO t_User_Outlook_Contacts_Loaded ([UserName],[Name_ID], [Action]) Values (fosusername(),'" & rst("Name_ID") & "', '" & [Action] & "')"
'Move to the next record
rst.MoveNext
'Start over again with the next record in the table/query
End If
Loop
'clean up
Set olContact = Nothing
Set con = Nothing
Set fld = Nothing
Set nms = Nothing
Set appOutlook = Nothing
rst.Close
Set rst = Nothing
MsgBox "Done"
HandleExit:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
MsgBox Err.Number & vbCrLf & Err.Description
Resume HandleExit
Resume
End Select
End Sub