Friday, October 19, 2012

Troubleshooting Microsoft Excel cannot access the file

Here’s the scenario. I am running SQL Server (on Windows Server 2008 R2) and I have a created a Job that has among other steps a CmdExec step. This CmdExec step calls a C# console application that I wrote. My C# console application calls the Microsoft.Office.Interop.Excel.Application. The code throws the following exception / message.

Microsoft Excel cannot access the file 'c:\myDir\MyFile.xlsx'. 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.


The last line of the code below is the line that throws the exception

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
string filename = @"c:\myDir\MyFile.xlsx"
Console.WriteLine("Exists: " + File.Exists(filename).ToString());
var wb = xlApp.Workbooks.Open(filename);

The message suggests that it can’t find my file that I am trying to open, so I made sure none of these were the issue. However, the actual problem is not anything to do with my file.  Instead I just had to create a directory called Desktop at the following path:


It is worth noting I am running Windows Server 2008 R2 and is a 64-bit operating system. If you are running a 32-bit OS you would need to change make sure there is a desktop directory here:


I noticed that I needed to be admin to follow the path above, but it didn’t seem to matter from a code perspective. However, if this solution doesn’t work, I would suggest playing with the permissions to that Desktop directory just to make sure since that is what I did to start with and then un-did my changes to the permissions. Also, I am running Windows 7 64-bit on my laptop where I am developing and I did not have any issues if when that directory did NOT exist. I don’t know why this seems to be particular to the server environment.

Thanks to this discussion that gave me this solution.


Anonymous said...

Thanks a lot! You saved my life!

Junior J. Zegarra said...

Gracias!!!.... excelente solución!!.

Un tema adicional si el Office esta en idioma espanol en vez de crear la carpeta DESKTOP debes crear la carpeta ESCRITORIO y funciona!

Anonymous said...

Thanks! I had so much pain because of this problem.

Anonymous said...

Thank you so much! Just migrated jobs from Windows Server 2003 to 2008 R2 and hit this snag when running from Task Scheduler job. THANKS AGAIN ! Bill In San Diego.

Anonymous said...

Lifesaver ! Thanks very much.
Muchisimas gracias!

Anonymous said...

Great! It finally works! Thank you very much!

Let me note that in my place, adding the c:\windows\syswow64\config\systemprofile\desktop
directory didn't work.

The point is that WOW64 stands for Windows on Windows64, that means it actually applies for 32-bit programs running on the 64bit OS.

Since I have 64-bit Excel installed, the proper directory turned out to be the

Anonymous said...

Thanksss a million ! Was struggling with this.. saved in time!

Stacey Voigt said...

Best solution ever!!! You are an absolute life saver

Venkatesh Chittepu said...

Very thankful to the died-line only because of this post today.

schouty said...

Thanks man! You saved my life. It worked after creating those folders.

trader said...

this is bonkers, but your fix works!

Anonymous said...

What if you don't permission to create a directory in that path

Used PC Dealer said...

Nice Blog Post !

sriram said...

Really cool post, highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you for this rare info!
MS office course in chennai

Praylin S said...

Remarkable post! I really like the way you've presented the article. Do share more such informative posts.
Manual Testing Training in Chennai
Manual Testing Training in Velachery
C C++ Training in Chennai
Spark Training in Chennai
Tally Course in Chennai
LINUX Training in Chennai