HOWTO: Automate Outlook using Visual BasicID: Q220595
|
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:
Outlook Version | How Type Library Appears in References List | Filename |
---|---|---|
Outlook 97 | "Microsoft Outlook 8.0 Object Library" | msoutl8.olb |
msoutl8.olb | "Microsoft Outlook 98 Object Library" | msoutl85.olb |
Outlook 2000 | "Microsoft Outlook 9.0 Object Library" | msoutl9.olb |
' 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")
olNs.Logon
' Create and Open a new contact.
Dim olItem As Outlook.ContactItem
Set olItem = olApp.CreateItem(olContactItem)
' Setup Contact information...
With olItem
.FullName = "James Smith"
.Birthday = "9/15/1975"
.CompanyName = "Microsoft"
.HomeTelephoneNumber = "704-555-8888"
.Email1Address = "someone@microsoft.com"
.JobTitle = "Developer"
.HomeAddress = "111 Main St." & vbCr & "Charlotte, NC 28226"
End With
' Save Contact...
olItem.Save
' 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...
With olAppt
.Duration = 60
.Subject = "Meeting to discuss plans..."
.Body = "Meeting with " & olItem.FullName & " to discuss plans."
.Location = "Home Office"
.ReminderMinutesBeforeStart = 1
.ReminderSet = True
End With
' Save Appointment...
olAppt.Save
' 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."
olMail.Send
' Clean up...
MsgBox "All done...", vbMsgBoxSetForeground
olNS.Logoff
Set olNs = Nothing
Set olMail = Nothing
Set olAppt = Nothing
Set olItem = Nothing
Set olApp = Nothing
For more information on Office Automation, please visit the Microsoft Office Development support site at:
http://support.microsoft.com/support/officedev/
Q166368 OL97: How To Get Help Programming with Outlook
© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Joe Crump, Microsoft Corporation
Keywords : kbAutomation kbOutlookObj kbVBp kbVBp500 kbVBp600 kbGrpDSO kbOffice2000
Version : WINDOWS:2000,5.0,6.0,97,98; :2000
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: June 30, 1999