Datasheet
670
Adding a Contact to Outlook
‘Set Contact Properties and Save
With OlkContact
.FirstName = Me.FirstName
.LastName = Me.LastName
Me.Email.SetFocus
.Email1Address = Me.Email.Text
.CompanyName = Me.Company
.BusinessAddressStreet = Me.Address1
.BusinessAddressCity = Me.City
.BusinessAddressState = Me.StateProv
.BusinessAddressPostalCode = Me.ZIPCode
.BusinessTelephoneNumber = Me.Phone
.BusinessFaxNumber = Me.Fax
.Save
End With
‘Let User know contact was added
MsgBox “Contact Added to Outlook.”
‘Clean up object variables
Set OlkContact = Nothing
Set Olk = Nothing
End Sub
This might look like a lot of code, but it’s just a series of small steps. In lay-
men’s terms, this procedure creates and sets a variable for the Outlook
Application, creates and sets a variable for an Outlook contact, sets the
properties of the Outlook contact object to values from the form, saves the
Outlook contact, displays a message box, and cleans up the object variables.
Let’s take a detailed look into this example.
The first two statements under the first comment declare an object variable
named Olk and set it to an open instance of Microsoft Outlook.
‘Open Instance of Microsoft Outlook
Dim Olk As Outlook.Application
Set Olk = CreateObject(“Outlook.Application”)
The Application object for Outlook lets you create items within Outlook,
just as if you open Outlook and navigate through the program. The second
comment and the next two lines are as follows:
‘Create Object for an Outlook Contact
Dim OlkContact As Outlook.ContactItem
Set OlkContact = Olk.CreateItem(olContactItem)
These lines declare an object variable named OlkContact and create that
contact using the CreateItem method of the Outlook Application object.
This is the VBA way of clicking Contacts and clicking the Click Here to Add a
New Contact line at the top of the Outlook window.
44_532188-bk09ch01.indd 67044_532188-bk09ch01.indd 670 3/29/10 10:59 PM3/29/10 10:59 PM










