w3dev w3dev - 24 days ago 10
C# Question

Using Outlook Office.Interop from a C# windows service

I just created a C# Desktop application for my personal use which reads my outlook calendar using

Office.Interop
libraries and posts all items to the Google calendar. It worked fine until I tried to convert it to a windows service. It always throws following error:

The file E:\...\Outlook Data File.pst cannot be opened.


I tried with different user accounts, but nothing worked. At the same time, the desktop application works as expected. Any idea?

Code:

Microsoft.Office.Interop.Outlook.Application application = null;
Microsoft.Office.Interop.Outlook.NameSpace applicationNamespace = null;
Microsoft.Office.Interop.Outlook.MAPIFolder folder = null;
Microsoft.Office.Interop.Outlook.Items calendarItems = null;

application = new Microsoft.Office.Interop.Outlook.Application();
applicationNamespace = application.GetNamespace("MAPI"); ;
folder = applicationNamespace.GetDefaultFolder(
Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar);
calendarItems = folder.Items;

foreach (
Microsoft.Office.Interop.Outlook.AppointmentItem item in calendarItems)
{
// process item
}

Answer Source

It seems that Interop is not supported in an unattended, non-interactive application:

Related Stackoverflow answer: https://stackoverflow.com/a/13480267/1716736
MS article: http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2