Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting following error while running a windows service in a remote machine.
The OS is Windows 7 (64 bit). Permission is there.
The path is correct. The file is not open either. Error occuring on opening the workbook.

C#
Microsoft Office Excel cannot access the file '\\ntp-228\SMART_PDF_FILE_STORE\TEMPLATE\T_001.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.


My code is as below:
C#
_Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                          xlApp.DisplayAlerts = false;
                          //fetch the Quotation No after Splitting pdf data
                          object misValue = System.Reflection.Missing.Value;
                          _Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
                          //read the Standard Excel Template fILE(Path Included)(Renamed) set in the Template Folder configured in App.Config
                          xlWorkBook = xlApp.Workbooks.Open(ExcelTemplateFullPath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
                          _Worksheet xlWorkSheet = (_Worksheet)xlWorkBook.Worksheets.get_Item(1);
Posted
Comments
Jochen Arndt 15-Jan-16 8:32am    
What Excel version do you use?

Not a solution but a tip to narrow down the source:

Try to open the file as normal file. If that fails it is file/access/network related and the returned error may be more informative. If that succeeds, it is an Excel problem.
F-ES Sitecore 15-Jan-16 8:45am    
As the file is on a network share check the service the code is running under has access to that share.
Michael_Davies 15-Jan-16 8:55am    
What context is the service running under: Local System or Local Service or one of the user accounts, the network and local resources require access permissions try running the service with a user account that has permission on the resource, might have to be an admin account as well in order to run as service.

1 solution

OK, first and foremost, Microsoft does not support and actively discourages using Office Interop in non-user applications, i.e.: Services and web applications.

Office was not written to support a non-interactive environment. You can have problems using Office Interop in your service.

Secondly, you're trying to open a file on a network share. If the account your service is running under does not have permissions to that share you're not going to be able to open the file. Chances are really good that you're running your service under Local System, which will not have access to any network shares at all.

The solution to that is to create a user account that has access to that share, give the account the LogonAsService permission and start your service under that account instead.
 
Share this answer
 
Comments
hypermellow 15-Jan-16 10:23am    
+5 for mentioning that the use of MS Office automation is not a good idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900