Microsoft Office Tutorials and References
In Depth Information
Adding Contacts to Outlook
Examining the contact-form code
The first two statements below 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 though you’d opened Outlook and navigated 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 by using the CreateItem method of the Outlook Application object.
This code is VBA’s way of clicking Contacts and then clicking the Click Here
to Add a New Contact line at the top of the Outlook window.
Now look at the next block of code:
‘Set Contact Properties and Save
.FirstName = Me.FirstName
.LastName = Me.LastName
.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
The With…End With block of code sets the properties of the Outlook
ContactItem. The ContactItem object has many properties that you can
see via the Object Browser. This example uses only a few of these properties
and sets them to the values from the form. Everything that uses the Me
keyword reads a value from the form shown in Figure 1-3, earlier in this chapter.
The last few statements tell the user that the contact was added and reset
the object variables.