While I have a way to export a single contact from Access to Outlook, I am finding the need to be able to export the ENTIRE list to a user's Outlook contacts.
I need help with the code that would loop thru all of the records, checking to see if a record exists or not (using CustomerID field) and if no record exists creating the contact. Then after it saves, automatically moving to the next record.
I've cobbled together something (below) but because the code came from a different process to import into Outlook vice export from Access, it doesn't compile past the following section (inumcontacts = objitems.Count).
I've commented where I think I have issues.
I need help with the code that would loop thru all of the records, checking to see if a record exists or not (using CustomerID field) and if no record exists creating the contact. Then after it saves, automatically moving to the next record.
I've cobbled together something (below) but because the code came from a different process to import into Outlook vice export from Access, it doesn't compile past the following section (inumcontacts = objitems.Count).
I've commented where I think I have issues.
Code:
Private Sub ExportAccessContacts_Click()
On Error GoTo Error_Handler
Dim OlApp As Object
Dim olContact As Object
Const olContactItem = 2
'https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa210907(v=office.11)?redirectedfrom=MSDN
Set OlApp = CreateObject("Outlook.Application")
Set olContact = OlApp.CreateItem(olContactItem)
'https://office-watch.com/2010/matching-outlook-and-access-contacts/
'http://www.helenfeddema.com/Access%20Archon.htm
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 i As Integer
lngContactID = Nz(Me.[Name_ID])
strFile = "C:\Users\" & fOSUserName() & "\Pictures\AVC_Backend\Contacts\" & Me.First_Name & " " & Me.Last_Name & ".png"
Set appOutlook = GetObject(, "Outlook.Application")
Set nms = appOutlook.GetNamespace("MAPI")
Set fld = nms.GetDefaultFolder(olFolderContacts)
Set con = fld.Items.Find("[CustomerID] = " & lngContactID)
'Need something that counts the number of Access records. I think this isn't right.
inumcontacts = objitems.Count
If inumcontact <> 0 Then
For i = 1 To inumcontacts
'Move to the first record in the table/query
rst.MoveNext
'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
If Not TypeName(con) = "Nothing" Then
'Skip to next contact
'Need some code to do this
Else
'https://docs.microsoft.com/en-us/office/vba/outlook/concepts/forms/outlook-fields-and-equivalent-properties
With olContact
.CustomerID = Me.Name_ID
.FirstName = Me.First_Name
.MiddleName = Nz(Me.MI, "")
.LastName = Me.Last_Name
.FullName = DLookup("[FullName]", "[q_Contacts_Search]", "[Name_ID]=" & Me.Name_ID)
.FileAs = DLookup("[FullName]", "[q_Contacts_Search]", "[Name_ID]=" & Me.Name_ID)
If IsNull(Me.Work_Anniversary) = False Then
.Anniversary = Format(Me.Work_Anniversary, "dd/mm/yyyy")
End If
If IsNull(Me.Birthday) = False Then
.Birthday = Format(Me.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(Me.Org_Child_ID, 0)), "")
.JobTitle = Nz(Me.JobTitle, "")
.BusinessAddressStreet = Nz(Me.Address, "")
.BusinessAddressCity = Nz(Me.City, "")
.BusinessAddressState = Nz(Me.State, "")
.BusinessAddressCountry = Nz(DLookup("[Country]", "[t_Country]", "[Country_Auto]=" & Nz(Me.Country, 0)), "")
.BusinessAddressPostalCode = Nz(Me.ZIP_Postal_Code, "")
.BusinessTelephoneNumber = Nz(Format(Me.Business_Phone, "(###)###-####"), "")
.MobileTelephoneNumber = Nz(Format(Me.Mobile_Phone, "(###)###-####"), "")
.Email1Address = Nz(Me.Email_Address, "")
.Email2Address = Nz(Me.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(Notes), "") & vbCrLf & "Skype: " & Nz(Skype, "")
.WebPage = Nz(Web_Page, "")
.ManagerName = Nz(DLookup("[FullName]", "[q_Contacts_Search]", "[Name_ID]=" & Nz(Me.Manager_Name_ID, 0)), "")
.AssistantName = Nz(DLookup("[FullName]", "[q_Contacts_Search]", "[Name_ID]=" & Nz(Me.Assistant_Name_ID, 0)), "")
If Dir(strFile) <> "" Then
.AddPicture (strFile)
.Save
'Move to the next record
Next i
'Do I need this?
rst.Close
rst.MoveNext
'Start over again with the next record in the table/query
Loop
End If
End With
End If
Error_Handler_Exit:
On Error Resume Next
If Not olContact Is Nothing Then Set olContact = Nothing
If Not OlApp Is Nothing Then Set OlApp = Nothing
Exit Sub
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: AddOlContact" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Sub