Microsoft Office Outlook Programming Using VSTO 3.0 and C#: Part 2

Get to grips with Programming Office 2007 using Visual Studio Tools for Office

Working with Appointments

Before we start working with the Appointments feature in Microsoft Office Outlook 2007, let's take a look at the Microsoft Office Outlook calendar. This will help you understand the concepts of Appointments more easily, and also explain how you can utilize this functionality for your needs. The Microsoft Outlook 2007 calendar is the scheduling component of the Outlook mail management system. It is well-integrated with other Microsoft Outlook functionality such as email, contacts, appointments, and other items in.

Appointments are the actions you're scheduling in your Outlook calendar, inviting other people to participate if required. You can set the status of your availability for an appointment, and you can also schedule recurring appointments.

Let's create an Outlook Appointment dynamically by using VSTO objects and C# programming:

  1. Open Visual Studio 2008, to create a new Outlook 2007 Add-in template project.
  2. Select New Project. Under Office select 2007 and select Outlook 2007 Add-in template and name the project as per your requirement.
  3. The solution will be created with all of supporting files required for the development of our Outlook solution.
  4. Write the following code, which will dynamically create an Appointment item in the ThisAddIn.cs file:
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    // Outlook AppointmentItem object to compose new Appointment
    Outlook.AppointmentItem PacktAppointmentItem = (Outlook.
    // Set the subject property value
    PacktAppointmentItem.Subject = "Regarding book review";
    // Set the location property value
    PacktAppointmentItem.Location = "Meeting Hall";
    // Set the start date
    PacktAppointmentItem.Start = DateTime.Today;
    // Set the end date
    Pac ktAppointmentItem.End = DateTime.Today;
    // Set the body property value
    PacktAppointmentItem.Body = "Book review comments from
    all editors";
    // Set the required attendee information
    PacktAppointmentItem.RequiredAttendees = "";
    // Set the optional attandee information
    PacktAppointmentItem.OptionalAttendees = "";
    // If parameter is set to false compose Appointment won't display
    // To send the composed PacktAppointmentItem

The following screenshot shows the results of adding and executing this code:

VSTO 3.0 for Office 2007 Programming

The AppointmentItem object is used to create appointments dynamically. An AppointmentItem object can be used to create a meeting, a one-time appointment, or a recurring appointment.

Let's perform a demonstration of how to delete a recurring appointment from your Outlook 2007 calendar, by using VSTO programming.

Open Visual Studio 2008 and create a new solution, as described earlier. Write the following code, which will dynamically delete an Appointment item, inside the ThisAddIn.cs file:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
// Reading the calendar folder through MEPIFolder object
Outlook.MAPIFolder PacktCalendarInfo = Application.Session.
// Get the data items in the calendar folder
Outlook.Items PacktCalendarDataItems = PacktCalendarInfo.Items;
// Searching the Appointment items based on subject
Outlook.AppointmentItem PacktAppointmentItem =
PacktCalendarDataItems["Book release"] as Outlook.
// Selected appointment's recurrence information
Outlook.RecurrencePattern PacktRecPattern = PacktAppointmentItem.
// Loading the appointment to AppointmentItem Object
Outlook.AppointmentItem PacktAppointmentDelete = PacktRecPattern.
GetOccurrence(new DateTime(2008, 9, 28, 8, 0, 0));
// Now delete using the Delete method

Working with meetings

Meetings are generally discussions amongst more than two people, during which predetermined topics are discussed. Meetings help you prepare a plan, or finalize pending work, or perform other tasks involving colleagues. In Microsoft Office Outlook, a meeting is a scheduled appointment—that is, people are invited to attend. You can set the meeting time and other options for the meeting attendees, to process the invitation.

VSTO 3.0 supports the dynamic creation of meeting items for Office. Let's create a meeting invitation dynamically, by using the VSTO object model and C# programming.

Open Visual Studio 2008 and create a new solution, as explained earlier. Write the following code, which will dynamically create a meeting invite item, inside the ThisAddIn.cs file:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
// Outlook PacktMeetingItem object to compose new meeting request
Outlook.AppointmentItem PacktMeetingItem = (Outlook.
PacktMeetingItem.MeetingStatus = Microsoft.Office.
// Set the subject for the meeting
PacktMeetingItem.Subject = "Changes in book content";
// Update the body information of the meeting
PacktMeetingItem.Body = "Work on the changes and update";
// Start Expiry Time of the meeting
PacktMeetingItem.Start = new DateTime(2008, 9, 28, 9, 0, 0);
// Set the recipient information
Outlook.Recipient PacktRecipient = PacktMeetingItem.Recipients.
Add("Radhika Rajagopalan");
PacktRecipient.Type = (int)Outlook.OlMeetingRecipientType.
// If parameter is set to false compose MeetingItem won't display
// To send the composed PacktMeetingItem

As we can see in the following screenshot, a Meeting tab is created successfully after executing this program.

VSTO 3.0 for Office 2007 Programming

An Outlook meeting is one of the many types of Appointments in Outlook. Meetings are internally linked with the Outlook calendar. A meeting request can be created using only the AppointmentItem object. To create and set the meeting invitation by using the AppointmentItem, you must set the MeetingStatus property to olMeeting.

Creating a Ribbon menu for Outlook 2007

The Ribbon is the new way of presenting menus for Office users and organizing related commands; visually it will appear as controls. The Ribbon menu feature is supported in most of the Office 2007 applications, such as Word, Excel, and Outlook. InfoPath and Visio are not provided with the Ribbon menu feature.

Let's create a Ribbon menu for Outlook by using Outlook objects and Visual designer support. In this example, we will create a Ribbon menu with a button on it. When you click the button, the option for composing a new mail will open.

  1. Open Visual Studio 2008 and create a new solution, as described earlier.
  2. Next, let's add the Ribbon component to our solution. Right-click on the project, and select Add | New Item... | Ribbon (Visual Designer), from the context menu. Name the Ribbon component as Ribbon1.cs, and click OK.

    Ribbon Visual Designer is a control that provides a visual designer for basic Ribbon customization tasks.

    VSTO 3.0 for Office 2007 Programming

  3. Expand the Toolbox sliding window in Visual Studio 2008, and you can find the controls that support the Ribbon menu under Office customization.
  4. VSTO 3.0 for Office 2007 Programming

  5. Next, drag-and-drop the controls that are required for your development inside your group control in the Ribbon. In this example, you are going to use the Button control.
  6. The Outlook Ribbon menu is quite different from other Office applications. The Ribbon menu varies for each Outlook region. For example, for reading a mail, you will see different Ribbon menu commands to those for an appointment and so on. To view your add-in that has the Ribbon menu, you need to specify the Ribbon types.

    VSTO 3.0 for Office 2007 Programming

  7. To create a program that will open a window for composing a new mail on a button click event, write the following code snippet inside the Ribbon1.cs file. Also include the code using Microsoft.Office.Interop.Outlook; in the Ribbon1.cs file to get access to the Outlook objects.
    // Click event of the button
    private void Button_OpenNewMail_Click(object sender,
    RibbonControlEventArgs e)
    // Application class to get Outlook object references
    ApplicationClass PacktApplication = new ApplicationClass();
    // Get the MAPIFolder NameSpaces
    NameSpace PacktNameSpace = PacktApplication.
    // Access to the default folders
    MAPIFolder ApreeMAPI = PacktNameSpace.
    // Outlook mailitem object to compose new mail
    MailItem PacktMailItem = (MailItem)ApreeMAPI.Items.
    // To display the new mail compose window

Adding and executing the preceding code results in the following output:

VSTO 3.0 for Office 2007 Programming

Outlook 2007 data interaction with Microsoft SQL Server 2008

Microsoft presents a programming interface for synchronizing data from an external data source with the Outlook data file dedicated to storing data from that source. As you know, VSTO 3.0 is powerful enough to use features of the .NET technology. Linking the Outlook contacts in a Microsoft SQL Server 2008 database is one of the key features available for Outlook users. Working with your Outlook contacts in a database will keep you informed of changes to Outlook contacts and vice versa.

Let's see how to make your Outlook 2007 application interact with a relational database management system, for example, Microsoft SQL Server 2008. Let's consider a scenario where you want to import all of the contact information for your friends or colleagues that is currently managed in a Microsoft SQL Server 2008 database.

  1. Open Visual Studio 2008 and create a new solution, as described earlier.
  2. You need to know about the database, and the details of the tables that you are going to import into your contact folder. You can see the table information that is used in this demonstration in the following images:

    VSTO 3.0 for Office 2007 Programming

    VSTO 3.0 for Office 2007 Programming

  3. Let's write a program to establish effective communication with the Microsoft SQL Server 2008 database table, and import the information to Outlook 2007's contact folder, by using the C# programming language and VSTO.
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    // Instantiated datatable, used to read the loaded data
    DataTable PacktDataTable = new DataTable();
    // Opening SQL connection for Microsoft SQL Server 2008
    SqlConnection PacktSQLConnection = new SqlConnection(@"Data
    Source=WINNER;Initial Catalog=PacktPub;Integrated
    // Passing SQL command text and SQL connection information
    SqlCommand PacktSQLCommand = new SqlCommand("SELECT * FROM
    Contacts", PacktSQLConnection);
    // Open the SQL connection through Open method
    // SQL reader to read through data from Database
    SqlDataReader PacktSQLReader = PacktSQLCommand.
    // Load the read data to datatable
    // Now close the datareader
    // Get the contact folder loaded using MAPIFolder object
    Outlook.MAPIFolder PacktContactFolder = Application.Session.
    // Accessing the Contact data items through Outlook item object
    Outlook.Items PacktContactItems = PacktContactFolder.Items.
    // To read the data one by one from the datatable
    foreach (System.Data.DataRow PacktDataRow in PacktDataTable.
    // Check if the current contact item exists in Outlook or not.
    Outlook.ContactItem PacktExistingContact = (Outlook.
    ContactItem)PacktContactItems.Find("[Email1Address] =
    '" + PacktDataRow["Email"] + "'");
    // If it exists, then delete
    if (PacktExistingContact != null)
    // Create a new contact object and update with data
    from database
    Outlook.ContactItem PacktAddContact = Application.
    CreateItem(Outlook.OlItemType.olContactItem) as
    // Assign the value from datarow value
    PacktAddContact.FirstName = PacktDataRow["FirstName"].
    PacktAddContact.Email1Address = PacktDataRow["Email"].
    PacktAddContact.CompanyName = PacktDataRow["Company"].
    // Save the assigned values as contact

Once executed, the information stored in the database is retrieved and displayed in the Contacts plane, as shown in the following screenshot:

VSTO 3.0 for Office 2007 Programming


This article examined some important topics regarding programming using VSTO and C# for Microsoft Office Outlook 2007. We learned programming for a meeting and an appointment in Outlook. We have learned about the Ribbon, and how we can create Ribbons for Outlook. We also learned the concept of database interaction with an Outlook application, with the help of an example of how to connect to a database and get information from it into the Contact folder of Outlook.

Books to Consider

comments powered by Disqus

An Introduction to 3D Printing

Explore the future of manufacturing and design  - read our guide to 3d printing for free