Public Function AddOlContact()
'Ref: https://docs.microsoft.com/en-us/office/vba/api/outlook.contactitem
On Error GoTo Error_Handler
Dim MyDB As Database, rs As Recordset
Dim CustomerID As Long
Dim rstFiltered As DAO.Recordset
Dim FName As String
Dim LName As String
Dim Address As String
Dim Address2 As String
Dim City As String
Dim State As String
Dim ZIP As String
Dim CustomerTel As String
Dim CustomerEmail As String
Dim TransferredTo As Boolean
Dim lngRSCount As Long
Dim TestMsg As Long
DoCmd.SetWarnings False
DoCmd.OpenQuery "Delete Temp Email and Tel Table for Outlook"
DoCmd.Close acQuery, "Delete Temp Email and Tel Table for Outlook"
DoCmd.OpenQuery "Append FName to TempOutlook Table"
DoCmd.Close acQuery, "Append FName to TempOutlook Table"
DoCmd.OpenQuery "Append FName2 to TempOutlook Table"
DoCmd.Close acQuery, "Append FName2 to TempOutlook Table"
DoCmd.SetWarnings True
Set MyDB = DBEngine.Workspaces(0).Databases(0)
Set rs = MyDB.OpenRecordset("Temp Email and Tel Table for Outlook")
lngRSCount = rs.RecordCount
TestMsg = 0
rs.MoveLast
rs.MoveFirst
With rs
Do Until rs.EOF
MsgBox ("At the beginning of Do Until, TestMsg is " & TestMsg)
FirstName = Nz(DLookup("FName", "Temp Email and Tel Table for Outlook"))
LastName = Nz(DLookup("LName", "Temp Email and Tel Table for Outlook"))
AddrStreet = Nz(DLookup("Address", "Temp Email and Tel Table for Outlook"))
AddrStreet2 = Nz(DLookup("Address2", "Temp Email and Tel Table for Outlook"))
AddrCity = Nz(DLookup("City", "Temp Email and Tel Table for Outlook"))
AddrState = Nz(DLookup("State", "Temp Email and Tel Table for Outlook"))
AddrZIP = Nz(DLookup("ZIP", "Temp Email and Tel Table for Outlook"))
CustomerTel = Nz(DLookup("Telephone", "Temp Email and Tel Table for Outlook"))
CustomerEmail = Nz(DLookup("Email", "Temp Email and Tel Table for Outlook"))
MsgBox ("123-The value of FName is: " & FirstName)
#Const EarlyBind = False 'True = Use Early Binding
'False = Use Late Binding
#If EarlyBind = True Then
'Early Binding Declarations
'Requires Ref to Microsoft Outlook XX.X Object Library
Dim oOutlook As Outlook.Application
Dim olContact As Outlook.ContactItem
#Else
'Late Binding Declaration/Constants
Dim olApp As Object
Dim olContact As Object
Const olContactItem = 2
#End If
Set olApp = CreateObject("Outlook.Application")
Set olContact = olApp.CreateItem(olContactItem)
With olContact
MsgBox ("TestMsg within olContact is: " & TestMsg)
.FirstName = FirstName
.LastName = LastName
.FullName = FirstName & ", " & LastName
.FileAs = LastName & ", " & FirstName
.JobTitle = JobTitles
.CompanyName = CompName
.HomeAddressStreet = AddrStreet
.HomeAddressCity = AddrCity
.HomeAddressState = AddrState
.HomeAddressPostalCode = AddrZIP
.BusinessTelephoneNumber = ContactTel
.Email1Address = CustomerEmail
.MobileTelephoneNumber = CustomerTel
.Save
' .Display 'Uncomment if you wish the user to see the contact pop-up
MsgBox ("Thank you, I have filed " & FirstName & " " & LastName & "'s contact information in Outlook.")
End With
TestMsg = TestMsg + 1
MsgBox ("First TestMsg is: " & TestMsg)
rs.MoveNext
MsgBox ("Second TestMsg is: " & TestMsg)
Loop
End With
rs.Close
MyDB.Close
Set rs = Nothing
Set MyDB = Nothing
Close
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 Function
Error_Handler:
MsgBox "The following error has occured" & 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 Occured!"
Resume Error_Handler_Exit
End Function