views:

3043

answers:

8

I’m well aware of the Microsoft support base article stating that it’s not supported to automate office products UI less. It seems that Windows Server 2008 x64 and Excel 2007 enforce the given statement.

I’m running the following code in a NT Service (Local System account) OnStart method. All it does is Excel automation the way it’s working when you run the same code in a Console Application.

The provided code has two parts. The first part launches Excel, creates a new work book and saves it to the given filename. The second part launches a new instance of Excel and opens the given file. The open operation ends in this exception:

Service cannot be started. System.Runtime.InteropServices.COMException (0x800A03EC): Microsoft Office Excel cannot access the file 'c:\temp\test.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.

Why was the automated excel able to launch and write files to disk but fails when it’s asked “just “ to open an existing file?

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
// launch excel and create/save a new work book
Microsoft.Office.Interop.Excel.ApplicationClass excel = new       Microsoft.Office.Interop.Excel.ApplicationClass();
excel.UserLibraryPath, excel.Interactive));
//            
string filename = "c:\\temp\\test.xls";
if(System.IO.File.Exists(filename)) System.IO.File.Delete(filename);
//
excel.Workbooks.Add(System.Reflection.Missing.Value);
excel.Save(filename);
excel.Quit();
excel = null;
// lauch new instance of excel and open saved file
excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
try
{
    Microsoft.Office.Interop.Excel.Workbook book = excel.Workbooks.Open(filename,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                true,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                false,
                false,
                System.Reflection.Missing.Value,
                false,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value);
     book.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
      book = null;
  }
  finally
  {
      excel.Quit();
      excel = null;
  }
  //
  GC.Collect();
+1  A: 

I've quite often found that calling Quit() isn't enough to release the resources. Try adding: -

System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

between the Quit() statement and setting it to null.

Stuart Whiteford
Thanks, guess what, I've dropped this part of the code to make the sample as simple as possible.BTW: ReleaseComObject returns an int and should be placed in loop
Chris Richner
Also nice to wait for the GarbageCollector if your program quits: GC.Collect();GC.WaitForPendingFinalizers();
sonstabo
+12  A: 
Chris Richner
Wow, that's it? I also had this problem and I thought it was do to Excel being run in Session 0. I used .Net remoting to run Excel under a user logged into Session 1...
wm_eddie
Both you and H Ogawa totally Rock. I was looking into the Session 0 situation as well, which appears to be a complete red herring.
Muhimbi
I am pleased to report this resolves the same issue under Windows 7 + IIS. Thank you for this fantastic solution!
Jeffrey Sharp
+1 for the easiest solution and most god awful bug to figure out.
Dayton Brown
+1 It fixed the same issue with Word for me
Friend Of George
THANK YOU!!! I was having the same problem in Windows 7 64bit with IIS7. Creating the C:\Windows\SysWOW64\config\systemprofile\Desktop fixed things for me. I'd previously tried doing the dcomcnfg.exe permissions suggestions too, which I found on other forum postings. So while I can't be sure the Desktop folder creation singlehandedly fixed my issues, I know for sure things started working as soon as I created that folder! Many thanks to Chris!
Dean L
A: 

oh bless you and your solution! windows server 2008 64bit and my Excel Com is my friend again! (except for the lack of oledb support ! )

+1  A: 

BRILLIANT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

A: 

THIS SAVED MY LIFE!!! TY

Ulf
A: 

This saved mine too ! Thank you so much. I precise that I'm in a windows 7 x64 environment and my problem was the same in word !

Sÿl
A: 

Thanks Ogawa.... I think i will never use OSIM

A Happy Guy
A: 

Just to add some more know how. The problem I have seen came along when running automated agents in Lotus Domino Server exporting data to Excel.

Your solution works perfect - thanks a lot.

But please tell me - how on earth did you work that out?

Klaus