Home > C#

Developing C# and Outlook 2007

26. September 2008

Overview

This article is going to be a part of a multi-part series on developing with C# and Outlook. The goal of this post is to connect to the Outlook API and create a task with categories. This article assumes you are using Visual Studio 2008 with the .NET Framework version 3.5. These articles are considered proofs-of-concept to aid you in developing better apps than I.

Create a Console Application

Open up visual studio and create a console application. We are creating a console application as apposed to an Outlook plugin to concentrate on the API itself allowing you to develop libraries, services, and plugins for Outlook. So I'm just going to put the basics out there and then explain what everything does.

  • Open up Visual Studio
  • Create a Console Application
  • Right click on the project and select Add Reference
  • Under the .NET tab click Microsoft.Interop.Outlook (the newest version)
  • Click ok

Use this code for the Program.cs file:

using System;
using Microsoft.Office.Interop.Outlook;

/// <summary>
/// This is the class acting as the entry
/// point for the application.
/// </summary>
class Program
{
    /// <summary>
    /// This is the entry point for the application
    /// </summary>
    /// <param name="args">Any arguments
    /// to pass into the application (not used).</param>
    static void Main(string[] args)
    {
        // Create an instance of the outlook application
        Application outlookApp = new ApplicationClass();
        // Get the namespace from the application
        NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI");

        // Create a task
        TaskItem task = (TaskItem)outlookApp.CreateItem(OlItemType.olTaskItem);

        // Set up some properties
        task.Subject = "My New Automated Task";
        task.Categories = "@Computer,@Home";

        // Save the task
        task.Save();
    }
}

Explanation

Hopefully the commenting will help the explanation. The first part creates an outlook application and assigns it to the Application interface. This application interface has access to versioning information as well as helper methods. We then get the namespace (folder) in which we are going to add the task to. We are passing in the type (outlook uses MAPI).

The create task is an important one. This line of code can be modified to create mail items, journal items, calendar items, contact items, etc. The outlookApp.CreateItem is a factory method to which we pass in the outlook item type. Finally, we call the save method for the item which places it in to your outlook.

Conclusion

This is a good start to get you on your way. I think this code is simple enough to allow you to fiddle with it and see what you can do. In the near future I will post about manipulating the more complex features of Outlook.



Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

C#

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading