Dela via


How to: Create a Custom Calendar

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

Microsoft Office version

  • Outlook 2003

  • Outlook 2007

For more information, see Features Available by Application and Project Type.

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.

Example

Private Sub CreateCustomCalendar()
    Const newCalendarName As String = "PersonalCalendar" 
    Dim primaryCalendar As Outlook.MAPIFolder = _
        Me.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook _
        .OlDefaultFolders.olFolderCalendar)
    Dim needFolder As Boolean = True 
    For Each personalCalendar As Outlook.MAPIFolder In _
        primaryCalendar.Folders()
        If personalCalendar.Name = newCalendarName Then
            needFolder = False 
        End If 
    Next 
    If needFolder Then 
        Dim personalCalendar As Outlook.MAPIFolder = _
            primaryCalendar.Folders.Add(newCalendarName)
        Dim newEvent As Outlook.AppointmentItem = _
            personalCalendar.Items.Add _
            (Outlook.OlItemType.olAppointmentItem)
        With newEvent
            .Start = Date.Now.AddHours(1)
            .End = Date.Now.AddHours(1.25)
            .Subject = "New plan"
            .Body = "Meet to discuss new plan."
            .Save()
        End With 
    End If 
    Me.Application.ActiveExplorer().SelectFolder(primaryCalendar _
        .Folders(newCalendarName))
    Me.Application.ActiveExplorer().CurrentFolder.Display()
End Sub
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();
}

See Also

Tasks

How to: Create Appointments

How to: Create a Meeting Request

Concepts

Working with Calendar Items