Edit

Share via


Programmatically create a custom calendar

This example creates a new Calendar folder named PersonalCalendar, and then creates a new Appointment item and adds it to the Calendar folder. The code then displays the Calendar folder.

Applies to: The information in this topic applies to VSTO Add-in projects for Outlook. For more information, see Features available by Office application and project type.

Example

private void CreateCustomCalendar()
{
    const string newCalendarName = "PersonalCalendar";
    Outlook.MAPIFolder primaryCalendar = (Outlook.MAPIFolder)
        this.Application.ActiveExplorer().Session.GetDefaultFolder
         (Outlook.OlDefaultFolders.olFolderCalendar);
    bool needFolder = true;
    foreach (Outlook.MAPIFolder personalCalendar
        in primaryCalendar.Folders)
    {
        if (personalCalendar.Name == newCalendarName)
        {
            needFolder = false;
            break;
        }
    }
    if (needFolder)
    {
        Outlook.MAPIFolder personalCalendar = primaryCalendar
            .Folders.Add(newCalendarName,
                Outlook.OlDefaultFolders.olFolderCalendar);
        Outlook.AppointmentItem newEvent =
            personalCalendar.Items.Add
            (Outlook.OlItemType.olAppointmentItem)
            as Outlook.AppointmentItem;
        newEvent.Start = DateTime.Now.AddHours(1);
        newEvent.End = DateTime.Now.AddHours(1.25);
        newEvent.Subject = "New plan";
        newEvent.Body = " Meet to discuss new plan.";
        newEvent.Save();
    }
    Application.ActiveExplorer().SelectFolder(primaryCalendar
        .Folders[newCalendarName]);
    Application.ActiveExplorer().CurrentFolder.Display();
}