How to: Open and Display the Contents of an iCalendar File
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
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.
Example
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.
If you use Microsoft Visual Studio to test this code sample, you must first add a reference to the Microsoft Outlook 12.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The Imports or using statements must not occur right before the functions in the Code section 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 in C#:
Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub OpenICalendarFile(ByVal fileName As String)
If String.IsNullOrEmpty(fileName) Then
Throw New ArgumentException(_
"Parameter must contain a value.", "exportFileName")
End If
If Not File.Exists(fileName) Then
Throw New FileNotFoundException(fileName)
End If
'' First try to open the icalendar file as an appointment
'' (not a calendar folder).
Dim item As Object = Nothing
Try
item = Application.Session.OpenSharedItem(fileName)
Catch
End Try
If Not item Is Nothing Then
'' Display the item
Dim olItem As OutlookItem = New OutlookItem(item)
olItem.Display()
Return
End If
'' If unsucessful in opening it as an item,
'' try opening it as a folder
Dim importedFolder As Outlook.Folder = Nothing
Try
importedFolder = TryCast( _
Application.Session.OpenSharedFolder(fileName), _
Outlook.Folder)
Catch
End Try
'' If sucessful, open the folder in a new explorer window
If Not importedFolder Is Nothing Then
Dim explorer As Outlook.Explorer = _
Application.Explorers.Add( _
importedFolder, _
Outlook.OlFolderDisplayMode.olFolderDisplayNormal)
explorer.Display()
End If
End Sub
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;
try
{
item = Application.Session.OpenSharedItem(fileName);
}
catch
{ }
if (item != null)
{
// Display the item
OutlookItem olItem = new OutlookItem(item);
olItem.Display();
return;
}
// If unsucessful in opening it as an item,
// try opening it as a folder
Outlook.Folder importedFolder = null;
try
{
importedFolder = Application.Session.OpenSharedFolder(
fileName, Type.Missing, Type.Missing, Type.Missing)
as Outlook.Folder;
}
catch
{ }
// If sucessful, open the folder in a new explorer window
if (importedFolder != null)
{
Outlook.Explorer explorer =
Application.Explorers.Add(importedFolder,
Outlook.OlFolderDisplayMode.olFolderDisplayNormal);
explorer.Display();
}
}