This article demonstrates how to programmatically control
Microsoft Outlook using Automation from Visual Basic. The example demonstrates
creating contacts, creating appointments, and sending messages by using
Microsoft Outlook's object-model.
Follow the steps below to create and run the example. To
run the sample, you need an early-bound reference to a Microsoft Outlook type
library. The following table lists the file names of the type libraries for the
different versions of Microsoft Outlook:
Collapse this tableExpand this table
|Outlook version||How type library appears in
|Outlook 97||"Microsoft Outlook 8.0 Object
|msoutl8.olb||"Microsoft Outlook 98 Object
|Outlook 2000||"Microsoft Outlook 9.0 Object
|Outlook 2002||"Microsoft Outlook 10.0 Object
|Office Outlook 2003||"Microsoft Outlook 11.0 Object
Building the automation sample
- Start Visual Basic, and create a new Standard EXE
- From the Project menu, choose References and select Microsoft Outlook.
- Add a button to your form.
- Double-click the button, and then add the following
' Start Outlook.
' If it is already running, you'll use the same instance...
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
' Logon. Doesn't hurt if you are already running and logged on...
Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")
' Create and Open a new contact.
Dim olItem As Outlook.ContactItem
Set olItem = olApp.CreateItem(olContactItem)
' Setup Contact information...
.FullName = "James Smith"
.Birthday = "9/15/1975"
.CompanyName = "Microsoft"
.HomeTelephoneNumber = "704-555-8888"
.Email1Address = "firstname.lastname@example.org"
.JobTitle = "Developer"
.HomeAddress = "111 Main St." & vbCr & "Charlotte, NC 28226"
' Save Contact...
' Create a new appointment.
Dim olAppt As Outlook.AppointmentItem
Set olAppt = olApp.CreateItem(olAppointmentItem)
' Set start time for 2-minutes from now...
olAppt.Start = Now() + (2# / 24# / 60#)
' Setup other appointment information...
.Duration = 60
.Subject = "Meeting to discuss plans..."
.Body = "Meeting with " & olItem.FullName & " to discuss plans."
.Location = "Home Office"
.ReminderMinutesBeforeStart = 1
.ReminderSet = True
' Save Appointment...
' Send a message to your new contact.
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
' Fill out & send message...
olMail.To = olItem.Email1Address
olMail.Subject = "About our meeting..."
olMail.Body = _
"Dear " & olItem.FirstName & ", " & vbCr & vbCr & vbTab & _
"I'll see you in 2 minutes for our meeting!" & vbCr & vbCr & _
"Btw: I've added you to my contact list."
' Clean up...
MsgBox "All done...", vbMsgBoxSetForeground
Set olNs = Nothing
Set olMail = Nothing
Set olAppt = Nothing
Set olItem = Nothing
Set olApp = Nothing
- Run the project, and click the button to run the
Once the code runs, you should have a new contact named "James
Smith," an appointment scheduled in two minutes with a reminder to appear in
one minute, and have sent a message to email@example.com. Also, because you
added a birthday for your contact (9/15), a recurring event was added for your
Outlook Calendar to remind you of the day.
New to Outlook 2002 are
the two dialog boxes: one warning you that a program is trying to access e-mail
addresses you have stored in Outlook and asking if you want to allow this, and
another message to the effect that a program is trying to send e-mail. This
feature will protect you from unknowingly being used by a virus that sends
e-mail from your system.
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
Description of the developer-related e-mail security features in Outlook 2002
For more information about Outlook programming resources, click the following article numbers to view the articles in the Microsoft Knowledge Base:
Resources for custom forms and programming in Outlook 97
Resources for custom forms and programming in Outlook 98
Resources for custom forms and programming in Outlook 2000
List of resources for custom forms and programming with Outlook 2002
How to retrieve contacts by using Outlook object model in Visual Basic .NET
How to create an appointment by using Outlook object model in Microsoft Visual Basic .NET
Article ID: 220595 - Last Review: March 15, 2006 - Revision: 9.4
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
- Microsoft Visual Basic 5.0 Learning Edition
- Microsoft Visual Basic 6.0 Learning Edition
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
- Microsoft Office Outlook 2003
- Microsoft Outlook 2002 Standard Edition
- Microsoft Outlook 2000
- Microsoft Outlook 97 Standard Edition
- Microsoft Outlook 98 Standard Edition
|kbautomation kbhowto kboutlookobj KB220595|