Search code examples
c#windows-server-2008scheduled-tasksexcel-interop

Can't schedule Program with Excel Interop


I have developed a console program in C# .NET, that uses Excel Interop. The program works just fine on my development machine and in the windows server 2008, if I run it from command line.

When I try to schedule a task to run it daily, I got this annoying Interop error:

02/11/2011 00:30:05,000 [1] FATAL My.Program [(null)] - Unable to Microsoft Office Excel open file 'E:\excel.xls' by one of several reasons: 

• The file name or path does not exist. 
• The file is being used by another program. 
• The workbook you are trying to save has the same name as the currently opened book. 

System.Runtime.InteropServices.COMException (0x800A03EC): Unable to access the Microsoft Office Excel file 'E:\excel.xls' by one of several reasons: 

• The file name or path does not exist. 
• The file is being used by another program. 
• The workbook you are trying to save has the same name as the currently opened book. 
  in Microsoft.Office.Interop.Excel.Workbooks.Open (String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, IgnoreReadOnlyRecommended Object, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad) 

The file 'e:\excel.xls' exists and if completely closed when the program starts, because if I try to run the same program in my command line, it ended as expected.

I also have setup my task to run as Administrator, so it shouldn't be permissions issues.

Can you help me?


Solution

  • Things can get a bit weird when you do Office Automation from a scheduled task, so I'd suggest trying a different user account.

    If it works when you're logged in, then I'd suggest setting up the task scheduler to run it under your account. If that still works, then create a new account with the same kind of permissions as your account and have it run under that account.

    Otherwise, try logging on as the administrator account and make sure that the app runs when logged in as administrator. For example, maybe there is some bit of Office that gets setup during the first interactive login by the user or similar.