问题
I have a custom application which uses Microsoft.Office.Interop.Excel.Application to open and save excel file. I have a batch which fires the app with all the required parameters. That batch completes the task successfully when run manually but when I tried to schedule the same in task scheduler I got the following error msg logged from ma app:
Microsoft Office Excel cannot access the file 'E:\tasks\extractSPdocs\downloads\Last_Minute_IT_DATA_DUMP_201404250000.xls'. There are several possible 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 a currently open workbook.
I know the error is thrown from the following code section:
try
{
excelfile = new Microsoft.Office.Interop.Excel.Application();
excelfile.DisplayAlerts = false;
var wb = excelfile.Workbooks.Open(dirpath + "\\" + csvname);
wb.SaveAs(dirpath + "\\" + csvname.Substring(0, csvname.LastIndexOf('.')), Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook);
csvname = csvname.Substring(0, csvname.LastIndexOf('.')) + ".xlsx";
csvext = ".xlsx";
}
The task is created with the same account I am using (local admin). It is set to run with highest privileges and to start in the batch file directory. The OS is Windows Server 2008.
I do not know why this is failing but suspect this has something to do with the context in which the scheduler launches my application which then subsequently launches Excel. Can anybody suggest a solution?
回答1:
I have found a solution here http://justgeeks.blogspot.co.uk/2012/10/troubleshooting-microsoft-excel-cannot.html
the trick is you have to craete this folder:
C:\Windows\SysWOW64\config\systemprofile\Desktop
I must admit I do not quite understand why this folder is required for excel to open files when launched by task scheduler but it works.
回答2:
Actually I haven't tried the answer by @Maju but this Superuser question has a solution which worked for me: you have to config DCOM. I set the identity to run Excel to the identity which I'm using in the Task Scheduler.
https://superuser.com/questions/579900/why-cant-excel-open-a-file-when-run-from-task-scheduler
回答3:
I added the following directory c:\windows\syswow64\config\systemprofile\desktop
来源:https://stackoverflow.com/questions/23290512/application-throws-an-error-when-launched-from-task-scheduler