In my last article ‘’, I have explained why we choose to use Excel Application object for excel automation and its disposing mechanism. But, while testing we were getting following exception:

Microsoft Excel cannot access the file '<file path>'. 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.:    at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)

But, file exists at that location.

After spending hours, I am able to find solution for this weird issue.

Basically solution is to create ‘Desktop’ folder under following paths on your machine:

If your machine is 64-bit then
- C:\Windows\SysWOW64\config\systemprofile

If your machine is 32-bit then
- C:\Windows\System32\config\systemprofile

 

My machine is 64-bit and I was already having ‘Desktop’ folder under ‘C:\Windows\SysWOW64\config\systemprofile’, but still it was not working.
Just to try out I created ‘Desktop’ folder under ‘C:\Windows\System32\config\systemprofile’ (which was not exist) on my machine.

And Bingo! Problem is solved. Exception went away and it is updating excel file correctly.

Really strange but it worked!

Looks like it creates some temporary file under ‘Desktop’ folder. But not too sure. Also it has something to do with under which user credentials it runs the windows service and excel application object.

Note: Paths may vary on your machine depending on where ‘windows’ folder reside.

I got idea for this solution from following 2 articles:

http://social.msdn.microsoft.com/Forums/en-US/innovateonoffice/thread/b81a3c4e-62db-488b-af06-44421818ef91?prof=required

Another solution: (but this requires registry changes – which is not preferred to do it on production machine)
http://social.technet.microsoft.com/Forums/en-US/office2007deploymentcompatibility/thread/334c9f30-4e27-4904-9e71-abfc65975e23

Happy Programming!