I have a small .NET app that I'm running under Windows 2008 Server via the Task Scheduler. This application needs to open an excel file and then save it as csv. The task fails when I try to open the workbook. If I run it manually without the task scheduler running it, the app works fine.
I have it set to "Run with highest privileges" and have "Run weather user is logged on or not" checked.
My guess is that this process needs to interact with the desktop similar to check the "interact with desktop" flag on a service. But I have been unable to find a similar thing for scheduled tasks.
Here is code that is failing: (it fails on the workbook.open call)
public static void ConvertExcelToCsv(string source, string destination)
{
if (File.Exists(destination)) File.Delete(destination);
Application xl = new Application();
try
{
Workbook workbook = xl.Workbooks.Open(source, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Worksheet ws = (Worksheet)workbook.Sheets[1];
ws.SaveAs(destination, XlFileFormat.xlCSV, Type.Missing, Type.Missing, false, false, Type.Missing, Type.Missing, Type.Missing,true);
Marshal.ReleaseComObject(ws);
}
finally
{
xl.DisplayAlerts = false;
xl.Quit();
Marshal.ReleaseComObject(xl);
}
}