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:
C:\Windows\SysWOW64\config\systemprofile\Desktop
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:
C:\Windows\System32\config\systemprofile\Desktop
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.
14 comments:
Thanks a lot! You saved my life!
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!
Thanks! I had so much pain because of this problem.
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.
Lifesaver ! Thanks very much.
Muchisimas gracias!
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
c:\windows\system32\config\systemprofile\desktop
Thanksss a million ! Was struggling with this.. saved in time!
Best solution ever!!! You are an absolute life saver
Very thankful to you.meet the died-line only because of this post today.
Thanks man! You saved my life. It worked after creating those folders.
this is bonkers, but your fix works!
What if you don't permission to create a directory in that path
Nice Blog Post !
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
Post a Comment