The following procedure illustrates how easily one can use VBA to create an Outlook Contact using Late binding so no references are required. Also note, there are many more contact properties available to the VBA programmer, for a full list consult the Outlook Help file.
'---------------------------------------------------------------------------------------
' Procedure : AddOlContact
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Create a new Contact item in Outlook
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: Uses Late Binding, so none required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' None, I need to convert this into a proper function, but for right now I leave that
' to you. This is a great starting point.
'
' Usage:
' ~~~~~~
' Call AddOlContact
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 Unknown Initial Release
' 2 2010-09-03 Website Release
' 3 2018-08-28 Added Early/Late Binding Option
' Updated Error Handling Slightly
' Added Picture demonstration
' Updated Copyright
'---------------------------------------------------------------------------------------
Public Sub AddOlContact()
'Ref: https://docs.microsoft.com/en-us/office/vba/api/outlook.contactitem
On Error GoTo Error_Handler
#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
.FirstName = "Daniel"
.LastName = "Alba"
.FullName = "Alba, Daniel"
.FileAs = "D. Alba"
.Anniversary = #7/22/1975#
.JobTitle = ""
.CompanyName = "MINI CARDA"
.BusinessAddressStreet = "22 ClearPoint"
.BusinessAddressCity = "Pointe-Claire"
.BusinessAddressState = "Quebec"
.BusinessAddressCountry = "Canada"
.BusinessAddressPostalCode = "H9X 3A6"
.BusinessTelephoneNumber = "(514) 488-0956"
.BusinessFaxNumber = ""
.Email1Address = "mini@mini.com"
.MobileTelephoneNumber = ""
.AddPicture ("C:\Users\ItsMe\Pictures\home-2955065_960_720.jpg")
.Save
'.Display 'Uncomment if you wish the user to see the contact pop-up
End With
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 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 Function
Update
Things have changed since the article was first published. If you are experiencing issues with the CreateObject(… line, then see my post entitled: CreateObject(“Outlook.Application”) Does Not Work, Now What?
