Export (0) Print
Expand All

Handy Tasks Using Microsoft Office Outlook 2003 and Visual Basic .NET

Office 2003
 

Frank Rice
Microsoft Corporation

April 2004

Applies to:
    Microsoft® Office Outlook® 2003
    Microsoft Visual Basic® .NET

Summary: Outlook provides a number of objects and methods for taking advantage of its built-in capabilities. This article illustrates some of those to include programmatically creating appointments, retrieving unread messages from the Inbox, and retrieving contacts. (11 printed pages)

Contents

Introduction
Create an Appointment
Create a Meeting Request
Retrieve Unread Messages from the Inbox
Retrieve Contacts
Conclusion

Introduction

You can create custom Microsoft® Office Outlook® objects and manipulate those objects from within Microsoft Office Outlook 2003 or from another application. You can manipulate Outlook objects by using Microsoft Visual Basic®, Microsoft Visual Basic for Applications (VBA) and Microsoft Visual Basic .NET code. The Outlook object model exposes Outlook objects, which you can use to gain programmatic access to Outlook functionality. This article illustrates ways to perform some common tasks used in Outlook by using Visual Basic .NET.

Create an Appointment

This section describes how to create an appointment by using the Outlook 2003 Object Model in Visual Basic .NET. An appointment, which is the default calendar item, involves only a schedule and time and does not require other attendees or resources. Appointments are shown in the calendar in time slots corresponding to start and end times as shown below.

The workhorse in the code that follows, and any code that pertains to appointments or meetings, is the AppointmentItem object. An AppointmentItem object represents an appointment in the Calendar folder. An AppointmentItem object can represent one appointment, a meeting (an appointment with attendees and resources), or recurring appointments and meetings.

  1. Start Microsoft Visual Studio® .NET.
  2. On the File menu, click New, and then click Project.
  3. Click Visual Basic Projects under Project Types, and then click Console Application under Templates. By default, Module1.vb is created.
  4. Add a reference to the "Microsoft Outlook 11.0 Object Library". To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, click Microsoft Outlook 11.0 Object Library, and then click Select.
    3. Click OK in the Add References dialog box to accept your selections. If you are prompted to generate wrappers for the library that you selected, click Yes.
  5. In the Code window, replace all of the existing code with the following:
    Imports System.Reflection
    Imports Outlook = Microsoft.Office.Interop.Outlook
    
    Module Module1
    
        Sub Main()
            ' Create an Outlook application.
            Dim oApp As Outlook.Application = New Outlook.Application()
    
            ' Create a new AppointmentItem.
            Dim oAppt As Outlook.AppointmentItem = oApp.CreateItem(Outlook.OlItemType.olAppointmentItem)
    
            ' Set some common properties.
            oAppt.Subject = "Created using OOM in VB .NET"
            oAppt.Body = "Hello World"
            oAppt.Location = "Room 1201"
    
            oAppt.Start = Convert.ToDateTime("05/31/2004 9:00:00 AM")
            oAppt.End = Convert.ToDateTime("05/31/2004 1:00:00 PM")
            oAppt.ReminderSet = True
            oAppt.ReminderMinutesBeforeStart = 5
            oAppt.BusyStatus = Outlook.OlBusyStatus.olBusy
            oAppt.IsOnlineMeeting = False
    
            ' Save to Calendar.
            oAppt.Save()
    
            ' Clean up.
            oApp = Nothing
            oAppt = Nothing
    
        End Sub
    End Module
    
  6. Make any changes to the code to make it work for your situation.
  7. Press F5 to build and run the program.
  8. Verify that the appointment is created.

Create a Meeting Request

This section describes how to create a meeting request by using the Outlook Object Model in Visual Basic .NET. Just as an AppointmentItem object can represent a one-time appointment, it can also represent a one-time or recurring meeting. A meeting typically involves more than one person. It is created when an AppointmentItem object is sent to other attendees, who receive it in the form of a MeetingItem object in their Inbox folders. You can't send a MeetingItem object directly. Instead, Outlook creates a MeetingStatus object when a user sends an AppointmentItem with its status set to olMeeting. The following code illustrates its use.

  1. Start Visual Studio .NET.
  2. On the File menu, click New, and then click Project.
  3. Click Visual Basic Projects under Project Types, and then click Console Application under Templates. By default, Module1.vb is created.
  4. Add a reference to the "Microsoft Outlook 11.0 Object Library." To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, click Microsoft Outlook 11.0 Object Library, and then click Select.
    3. In the Add References dialog box, click OK to accept your selections. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  5. In the Code window, replace all of the code with the following:
    Imports System.Reflection
    Imports Outlook = Microsoft.Office.Interop.Outlook
    Module Module1
    
        Sub Main()
           ' Create an Outlook application.
            Dim oApp As Outlook.Application = New Outlook.Application()
    
            ' Create an AppointmentItem.
            Dim oAppt As Outlook._AppointmentItem = oApp.CreateItem(Outlook.OlItemType.olAppointmentItem)
    
            ' Change AppointmentItem to a Meeting. 
            oAppt.MeetingStatus = Outlook.OlMeetingStatus.olMeeting
    
            ' Set some common properties.
            oAppt.Subject = "Created using OOM in VB.NET"
            oAppt.Body = "Hello World"
            oAppt.Location = "Room 1201"
    
            oAppt.Start = Convert.ToDateTime("05/31/2004 9:00:00 AM")
            oAppt.End = Convert.ToDateTime("05/31/2004 1:00:00 PM")
    
            oAppt.ReminderSet = True
            oAppt.ReminderMinutesBeforeStart = 5
            oAppt.BusyStatus = Outlook.OlBusyStatus.olBusy 
            oAppt.IsOnlineMeeting = False
            oAppt.AllDayEvent = False
    
            'Add attendees. This statement will display the Addressbook
            'Security dialog box. For more information, see the 
            'security information in the article "What's 'New in
         'Microsoft Office Outlook 2003 for Developers"? The URL is
         'available at the end of this article.
            Dim oRecipts As Outlook.Recipients = oAppt.Recipients
    
            ' Add required attendee.
            Dim oRecipt As Outlook.Recipient
            oRecipt = oRecipts.Add("UserTest1")
            oRecipt.Type = Outlook.OlMeetingRecipientType.olRequired
    
            ' Add optional attendee.
            oRecipt = oRecipts.Add("UserTest2")
            oRecipt.Type = Outlook.OlMeetingRecipientType.olOptional
            oRecipts.ResolveAll()
    
            'Send out a request. The warning "A program is trying
            'to automatically send e-mail on your behalf." will appear.
      'with this command. For more information, see the article
      '"What's 'New in Microsoft Office Outlook 2003 for
      'Developers?" The URL appears at the end of this article.
            oAppt.Send()
    
            ' Clean up.
            oApp = Nothing
            oAppt = Nothing
            oRecipts = Nothing
            oRecipt = Nothing
    
        End Sub
    End Module
    
  6. Make any changes to the code to make it work for your situation.
  7. Press F5 to build and run the program.
  8. Verify that the meeting request was created.

Retrieve Unread Messages from the Inbox

This section describes how to use Microsoft Outlook 11.0 Object Library to retrieve unread messages from the Outlook Inbox in Visual Basic .NET. One of the methods used in the next procedure is the GetDefaultFolder method. The GetDefaultFolder method of the Namespace object accesses folders in the root folder, also known as the Mailbox. Mailbox folders contain all Outlook built-in and custom folders. One of the ways to add a reference to a folder in the Mailbox is to specify one of the enumerations with the GetDefaultFolder method, such as is the case of this procedure, olFolderInbox representing the Inbox.

  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic Projects under Project Types, and then click Console Application under Templates. By default, Module1.vb is created.
  4. Add a reference to the "Microsoft Outlook 11.0 Object Library." To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, click Microsoft Outlook 11.0 Object Library, and then click Select.
    3. In the Add References dialog box, click OK. If you are prompted to generate wrappers for the library that you selected, click Yes.
  5. In the Code window, replace the default code with the following code:
    Imports System.Reflection
    Imports Outlook = Microsoft.Office.Interop.Outlook
    Module Module1
    
        Sub Main()
            ' Create Outlook application.
            Dim oApp As Outlook.Application = New Outlook.Application
            ' String used for comparison with mail item.
            Dim sClassComp = "IPM.Note"
    
            ' Get Mapi NameSpace.
            Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
    
            ' Get Messages collection of Inbox.
            Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
            Dim oItems As Outlook.Items = oInbox.Items
            Console.WriteLine("Total : " & oItems.Count)
    
            ' Get unread e-mail messages.
            oItems = oItems.Restrict("[Unread] = true")
            Console.WriteLine("Total Unread : " & oItems.Count)
    
            ' Loop each unread message.
            Dim oMsg As Outlook.MailItem
            Dim i As Integer
            For i = 1 To oItems.Count
                'Test to make sure item is a mail item
                'and not a meeting request.
                If oItems.Item(i).MessageClass = sClassComp Then
                    oMsg = oItems.Item(i)
    
                    Console.WriteLine(i)
                    Console.WriteLine(oMsg.SenderName)
                    Console.WriteLine(oMsg.Subject)
                    Console.WriteLine(oMsg.ReceivedTime)
                    Console.WriteLine(oMsg.Body)
                    Console.WriteLine("---------------------------")
                End If
            Next
    
            ' Clean up.
            oApp = Nothing
            oNS = Nothing
            oItems = Nothing
            oMsg = Nothing
    
        End Sub
    End Module
    
  6. Make any changes to the code to make it work for your situation.
  7. Press F5 to build and to run the application.
  8. Verify that the unread messages are retrieved.

Retrieve Contacts

This section describes how to use the Outlook 11.0 Object Library to retrieve Outlook contacts in Visual Basic .NET. Similar to the procedure in the previous sample, the GetDefaultFolder method of the Namespace object accesses folders in the root folder. You can think of the NameSpace object as the gateway to all existing Outlook folders. In the following procedure, the GetDefaultFolder method retrieves contacts from the Contacts folder. The ContactItem object is also used to work with contacts. The ContactItem object represents a contact in a contacts folder. A contact can represent any person with whom you have any personal or professional contact.

Editor's Note   Based on customer feedback, we've updated this example from retrieving the first contact to retrieving all the contacts. Thanks for your comments!
  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic Projects under Project Types, and then click Console Application under Templates. By default, Module1.vb is created.
  4. Add a reference to the "Microsoft Outlook 11.0 Object Library." To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, click Microsoft Outlook 11.0 Object Library, and then click Select.
    3. In the Add References dialog box, click OK. If you are prompted to generate wrappers for the library that you selected, click Yes.
  5. In the Code window, replace the default code with the following code:
    Imports System.Reflection
    
    Module Module1
    
        Sub Main()
            ' Create Outlook application.
            Dim oApp As Outlook.Application = New Outlook.Application
    
            ' Get namespace and Contacts folder reference.
            Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
            Dim cContacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
    
            ' Get the first contact from the Contacts folder.
            Dim oItems As Outlook.Items = cContacts.Items
            Dim oCt As Outlook.ContactItem
    
            Dim iCount As Int16
            iCount = 0
    
            oCt = oItems.GetFirst()
    
            Do While Not oCt Is Nothing
                iCount += 1
    
                ' Display some common properties.
                Console.WriteLine(oCt.FullName)
                Console.WriteLine(oCt.Title)
                Console.WriteLine(oCt.Birthday)
                Console.WriteLine(oCt.CompanyName)
                Console.WriteLine(oCt.Department)
                Console.WriteLine(oCt.Body)
                Console.WriteLine(oCt.FileAs)
                'This next statement will display an Address 
                'Security warning. For more information, see the 
                'security information in the article at the end of this 
                'article titled "What's New in Microsoft Office 
                'Outlook 2003 for Developers"?
                Console.WriteLine(oCt.Email1Address)
                Console.WriteLine(oCt.BusinessHomePage)
                Console.WriteLine(oCt.MailingAddress)
                Console.WriteLine(oCt.BusinessAddress)
                Console.WriteLine(oCt.OfficeLocation)
                Console.WriteLine(oCt.Subject)
                Console.WriteLine(oCt.JobTitle)
                oCt = oItems.GetNext
            Loop
    
            Console.WriteLine(iCount)
    
            ' Clean up.
            oApp = Nothing
            oItems = Nothing
            oCt = Nothing
    
        End Sub
    End Module
    
  6. Make any changes to the code to make it work for your situation.
  7. Press F5 to build and to run the application.
  8. Verify that the code loops through each of your contacts.

Conclusion

Outlook contains a number of objects and methods that make it easy to customize the working environment for your own uses. Everything from working with different types of appointments such as one-time and recurring meetings to retrieving and manipulating contacts lists. This article provided just a hint of the possibilities that you can put together for your own purposes.

Additional Resources

Show:
© 2014 Microsoft