25

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();
Chris Richner
  • 2,863
  • 2
  • 26
  • 38
  • FYI: The same code works like a charm deployed to a Windows Server 2003 R2 x64 & Excel 2007. – Chris Richner May 18 '09 at 14:57
  • -1 for helping new readers try Office Automation from server processes. – John Saunders Feb 29 '12 at 03:25
  • @John Saunders. well, and what to do will the following requirement: run an 'update' _macro_ within an xls file, without automation? Just curious (not that such a design is a good idea to begin with, but users are users...) – Vinzz Jun 07 '12 at 10:46
  • @Vinzz: what would you do with any other requirement that cannot be met: Tell the truth. – John Saunders Jun 07 '12 at 12:55
  • @John Saunder: Alas, in this particular case, it can (and already was) met. But indeed, trying to convince another solution is a better idea is the right thing to do. – Vinzz Jun 07 '12 at 20:30
  • @Vinzz: you mean it's been met by running Excel in an unsupported scenario? I guess that's "met" if they didn't specify "supported" in the requirements. – John Saunders Jun 07 '12 at 21:15

5 Answers5

31

The solution is really simple. The msdn forum thread can be found here

To make a long story short I'm posting the solution here, credit goes to H Ogawa

This solution is ...

・Windows 2008 Server x64

Please make this folder.

C:\Windows\SysWOW64\config\systemprofile\Desktop

・Windows 2008 Server x86

Please make this folder.

C:\Windows\System32\config\systemprofile\Desktop

...instead of dcomcnfg.exe.

This operation took away office automation problems in my system.

A Desktop folder seems to be necessary in the systemprofile folder to open file by Excel.

It disappears from Windows2008, Windows2003 had the folder, and I think it cause this error.

Chris Richner
  • 2,863
  • 2
  • 26
  • 38
  • 2
    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 Aug 20 '09 at 04:14
  • 1
    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. – Jeroen Ritmeijer Sep 11 '09 at 16:05
  • I am pleased to report this resolves the same issue under Windows 7 + IIS. Thank you for this fantastic solution! – Jeff Sharp Oct 15 '09 at 16:42
  • 2
    +1 for the easiest solution and most god awful bug to figure out. – Dayton Brown Dec 29 '09 at 17:25
  • 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 Apr 23 '10 at 16:56
  • I posted a related question about getting this to work with a non-system user account. http://stackoverflow.com/questions/5226473/excel-2007-automation-on-top-of-a-windows-server-2008-x64-non-system-user – Suraj Mar 07 '11 at 23:19
  • @Chris - Have you been able to find an explanation as to why this folder needs to be created? It solved my issue but I would like to know why this folder is required. – thiag0 Apr 06 '11 at 20:35
7

Also like stated in the source, you need to set the correct rights for the Desktop folder. This worked for me on Windows 2008-64bits and Office 2010 32bits.

  1. Create directory "C:\Windows\SysWOW64\config\systemprofile\Desktop " (for 64 bit Windows) or "C:\Windows\System32\config\systemprofile\Desktop " (for 32 bit Windows)

  2. Assign user "Network Services (Service Réseau)" the following rights for the created folder:

Read & Execute, List folder contents, Read

John.

Jonx
  • 1,075
  • 12
  • 16
2

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
  • 521
  • 7
  • 22
  • 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 May 14 '09 at 20:00
  • 2
    Also nice to wait for the GarbageCollector if your program quits: GC.Collect(); GC.WaitForPendingFinalizers(); – sonstabo Feb 23 '10 at 11:26
1

If you are using Apache, you might also need to follow these steps to get MS Word working properly (along with everything outlined in other answers):

Below is a screenshot showing the two dialogs you'll need to bring up: enter image description here

For Apache:

Services->Apache->Right Click (Properties)->Log On Tab

MS Word:

Launch dcomcnfg.exe->Console Root->Component Services->Computers->My Computer->DCOM Config->Find Microsoft Application->Right Click(Properties)->Identity Tab

**if you can't find the MS Word, make sure you are launching the correct DCOM Config (64 bit vs 32 bit) depending on what version of Office you have installed.

There are two options here, you can set Apache to use Local System Account and check the checkbox to ALLOW desktop interaction. If you do this then you need to set the Identity for MS Word to Interactive User.

Otherwise, you need to set both to the same user (Ideally the user that is logged in) like shown in the picture.

A.O.
  • 3,733
  • 6
  • 30
  • 49
1

There are many more errors than the one mentioned that you'll need to work through in order to get Excel working on Windows Server 2007 64-bit. See the steps I worked out after working on this for two full days!

Borgon
  • 41
  • 3