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

by Vivek Thangaswamy | March 2009 | Microsoft

Primarily, the Microsoft Office Outlook serves as an email application for a wide range of users. In the previous part of the article, we took an Overview of the Outlook object model and its features in VSTO. We saw the extensibility of Microsoft Office Outlook 2007 using the object model. We also learned to customize Microsoft Office Outlook menus and toolbars using VSTO. In the last part, we worked with form regions in Outlook, how to manipulate folders, contact information, and mail items using VSTO programming.

In this article by Vivek Thangaswamy, we will take a look at the following topics:

  • Learning the concepts and seeing a demonstration of working with Outlook meetings and appointments
  • Working with Ribbons for Outlook 2007
  • Outlook applications and the Microsoft SQL Server 2008 database interaction

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.
    AppointmentItem)this.Application.CreateItem(Outlook.
    OlItemType.olAppointmentItem);
    // 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 = "vivek@vsto.com";
    // Set the optional attandee information
    PacktAppointmentItem.OptionalAttendees = "radhika@vsto.com";
    // If parameter is set to false compose Appointment won't display
    PacktAppointmentItem.Display(true);
    // To send the composed PacktAppointmentItem
    //((Outlook._AppointmentItem)PacktAppointmentItem).Send();
    }

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.
GetDefaultFolder(Outlook.OlDefaultFolders.
olFolderCalendar);
// 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.
AppointmentItem;
// Selected appointment's recurrence information
Outlook.RecurrencePattern PacktRecPattern = PacktAppointmentItem.
GetRecurrencePattern();
// 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
PacktAppointmentDelete.Delete();
}

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.
AppointmentItem)this.Application.CreateItem(Microsoft.Office.
Interop.Outlook.OlItemType.olAppointmentItem);
PacktMeetingItem.MeetingStatus = Microsoft.Office.
Interop.Outlook.OlMeetingStatus.olMeeting;
// 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.
olRequired;
// If parameter is set to false compose MeetingItem won't display
PacktMeetingItem.Display(true);
// To send the composed PacktMeetingItem
//((Outlook.MeetingItem)PacktMeetingItem).Send();
}

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.

Sign up for a Packt account to see the rest of this article

Now that you've read a few articles, you might want to consider signing up for a Packt account. It takes a matter of seconds, will give you access to all the articles on PacktPub.com, and once you've signed up you'll be returned here to carry on reading your article.

Furthermore, you'll gain access to nine free ebooks, and be offered a free trial of PacktLib, Packt's online library. Simply enter your details here, or log in to your existing account.

Log in

...or register

Open web page in outlook 2010 with visual studio by
Thanks for this article. I want to customize my outlook 2010 by adding a new tab on wich i add a button wich onclick have to open a web page in outlook. I try this code onclick of my button it didn't work below my code ; nsp = Application.GetNamespace("MAPI") mpfInbox = nsp.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) mpfNew = mpfInbox.Folders.Add("MyFolderHomePage") mpfNew.WebViewURL = "http://www.microsoft.com" mpfNew.WebViewOn = True Please help me.
Nice Article by
Thanks for this wonderful article. I was bit scared about outlook customization before reading these two posts, but after reading this i got good visibility to write custom utilities for outlook.

Post new comment

Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Sort A-Z