Microsoft Office Tutorials and References
In Depth Information
Adding Contacts to Outlook
Now, just adding the button doesn’t accomplish much; you also have to
add code to the button’s Click event procedure. In the form’s Design view,
double-click the button to show the property sheet, click the Event tab, click
the ellipsis button to the right of the On Click event property, and then
click Code Builder to open Visual Basic Editor to the button’s Click event
procedure. The code looks something like this:
Private Sub cmdOutlook_Click()
‘Open Instance of Microsoft Outlook
Dim Olk As Outlook.Application
Set Olk = CreateObject(“Outlook.Application”)
‘Create Object for an Outlook Contact
Dim OlkContact As Outlook.ContactItem
Set OlkContact = Olk.CreateItem(olContactItem)
‘Set Contact Properties and Save
With OlkContact
.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
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 example may look like a lot of code, but it’s really just a series of small
steps. In layman’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. In the following section, we give you a detailed look at this example.
Search JabSto ::

Custom Search