Export (0) Print
Expand All

How to: Open and Display the Contents of an iCalendar File

This example shows how to open and display the contents of an iCalendar file, depending on whether the file contains a single or recurrent appointment, or the file contains a group of appointments.

This code sample first tries to use the OpenSharedItem method of the NameSpace object to open the iCalendar file as a single-appointment iCalendar file. If it succeeds, it will display the appointment item details in an inspector window. However, it will not copy the item to the default store. If the sample cannot open the file as a single-appointment iCalendar file, then it will use the OpenSharedFolder method of the NameSpace object to attempt to import the contents as a new calendar in the default store. If it succeeds in importing, then it will display the calendar in an explorer window.

This code sample makes use of the OutlookItem helper class, defined in How to: Create a Helper Class to Access Common Outlook Item Members, to conveniently call the OutlookItem.Display method to display the appointment item without having to first cast the item.

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The Imports or using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following lines of code show how to do the import and assignment in Visual Basic and C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void OpenICalendarFile(string fileName)
    if (string.IsNullOrEmpty(fileName))
        throw new ArgumentException("exportFileName", 
        "Parameter must contain a value.");
    if (!File.Exists(fileName))
        throw new FileNotFoundException(fileName);

    // First try to open the icalendar file as an appointment 
    // (not a calendar folder).
    object item = null;
        item = Application.Session.OpenSharedItem(fileName);
    { }

    if (item != null)
         // Display the item
         OutlookItem olItem = new OutlookItem(item);

    // If unsucessful in opening it as an item, 
    // try opening it as a folder
    Outlook.Folder importedFolder = null;
        importedFolder = Application.Session.OpenSharedFolder(
            fileName, Type.Missing, Type.Missing, Type.Missing) 
            as Outlook.Folder;
    { }

    // If sucessful, open the folder in a new explorer window
    if (importedFolder != null)
        Outlook.Explorer explorer =

Other resources

© 2014 Microsoft