«

»

Jan 07

How to make IIS7 play nice with Office Interop

I’ve just started working on a new project for which we had to dig out one of our older libraries. We wrote it to retrieve data from MS Excel workbooks using the Office interop assemblies, and thus far it has never given us issues. A slight change this time round was that we were using our library in an ASP.NET site rather than a desktop application. As a result I was faced with two errors that we’d never seen before.

Error Message:

Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

Solution:

This is a COM permissions problem.

  1. Start > Run > dcomcnfg (or “mmc -32” and then add the Component Services snap-in manually if you can’t find the app under step 3)
  2. Navigate to Component Services > Computers > My Computer > DCOM Config
  3. Locate the MS application giving you trouble (eg: “Microsoft Excel Application” for Excel or “Microsoft Word 97 – 2003 Document” for Word)
  4. Right click > Properties
  5. On the security tab: Select Customize under Launch and Activation Permissions and click Edit…
  6. Add the account under which the site is running (eg: Network Service) and assign Local Launch & Local Activation permissions
  7. Voila!

Error Message:

[COMException (0x800a03ec): Microsoft Excel cannot access the file ‘<filename>’. 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.

Solution:

This seems to be an issue with Excel/Word/etc not having access to a profile when being started via the interop route.

  1. Open Windows Explorer
  2. Depending on whether you installed a 32bit or 64bit version of office you will need to do one  (or both) of the following:
    1. 32bit Office installation: Navigate to C:\Windows\System32\config\systemprofile
    2. 64bit Office installation: Navigate to C:\Windows\SysWOW64\config\systemprofile
  3. Verify the folder “Desktop” exists (create it if it’s not there)
  4. Right click > Properties
  5. On the security tab: Add the account under which the site is running (eg: Network Service) with default permissions (Read & execute; List folder contents; Read)
  6. Voila!

Many thanks to the contributors of this discussion.

 

Important!

Update:

As this stackoverflow question suggests, Microsoft have published a KB article explaining why you should not be doing this (there is no support, there are licensing issues etc.). They recommend that Open XML be used instead.

 

Error Message:
Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

Solution:
This is a COM permissions problem.

	1. Start > Run > dcomcnfg (or "mmc -32" and then add the Component Services snap-in manually if you can't find the app under step 3)
	2. Navigate to Component Services > Computers > My Computer > DCOM Config
	3. Locate the MS application giving you trouble (eg: "Microsoft Excel Application" for Excel or "Microsoft Word 97 - 2003 Document" for Word)
	4. Right click > Properties
	5. On the security tab: Select Customize under Launch and Activation Permissions and click Edit...
	6. Add the account under which the site is running (eg: Network Service) and assign Local Launch & Local Activation permissions
	7. Voila!

52 comments

1 ping

Skip to comment form

  1. mauricio

    hey Malte, great post. You made me safe lot of time. There is not a real post around here that expose a solution to this problem. Your post covers the two problems you have to deal with when working with the Microsoft.Office.Interop.
    Thanks!

    1. Malte

      Hi Mauricio, thanks for the feedback. I’m glad my post was helpful to you!

  2. SzilardD

    This is really helpful. Thanks so much.

  3. etatmirtna

    Unfortunately now my Excel will not start without errors about OLE happening. Trying to uninstall then reinstall…otherwise at least I was able to connect to the file and make a change in it

    1. Malte

      Hi etatmirtna, I’m afraid I haven’t experienced that problem before. Please let us know if you manage to fix it – and how! 🙂

  4. Jynx

    Good concise article Malte.

    Hope you don’t mind but I have linked this article from my blog http://jynxeddevelopment.blogspot.com/2011/07/microsoft-office-interop-tricks-and.html – it took me ages to work this out from other threads and posts so I thought I would create my own, then I found yours, wish I had found it first!

    Thanks.

    1. Malte

      Hi Jynx, not at all. Thanks for the feedback. It took me a while to piece all this together too, so I thought I might as well make it a little easier for folks. An issue that’s encountered often – judging by the popularity of this page alone. Looks like I need to add ‘windows service’ to the keywords.. I thought it was specific to IIS7.

      By the way, congrats on a great first blog post!

  5. Rajesh Hatwal

    Thnaks a lot, It really works. I tried the second solution in windows 7 32 bit and windows server 2008 32 bit

  6. Kalle

    You are a saviour… I have spent a couple of hours ripping my hair out over this and you solved it for me!

    1. Malte

      Great! Hope you still have some hair..

  7. Chinwendu P. Ochonma

    I have applied the first Solution to the COM error in my webservice application, but i still keep having the same error

    Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

    1. Malte

      Hi Chinwendu, the only thing I can think of is that perhaps you added permissions for the wrong account. You can check which account the site is running under by selecting the site in IIS Manager and then clicking Basic Settings in the actions pane. This will tell you which application pool the site is using. Now go to Application Pools and check the identity of that application pool. The identity is the account for which you need to add the permissions. If the identity is “ApplicationPoolIdentity”, check here for a good explanation of which account that actually maps to.

    2. Jesse HUnter

      The above step didn’t solve the access issue for me either. I had to also customize the Access Permissions and grant Local Access to my process.

  8. CBH

    Hello, I had followed your step, but I still got the same error. My Server use Window Server 2008 R2 (x64) OS and I had installed the Microsoft Office 2007 in the it.

    Any other solutions.

    Thanks you in advance.

    1. Malte

      Hi there,
      which error is it exactly that you’re getting? You might want to try the mmc -32 way if you’re getting the ‘COM class factory’ error and Office is 32-bit. Also check out the discussion I linked to at the end of the post.

  9. Chi

    Hi Malte,

    I followed the instruction on your post. It partial resolves my web service the 2 errors that you mentioned above. However, my web service still have problem with launching the Excel and cannot run the Macro. I wonder if you have any ideas why Excel does not launch.

    I have my web service run on iis 7.5, windows 7 64 bit. .NET framework 3.5, Microsoft office 2010 (32 bit)

    These are the items that I did:
    1. follow the above instructions to use DCOM to grant the NETWORK_SERVICE account permission to activate, access and launch the Excel locally.
    2. create Desktop folder under C:\Windows\SysWOW64\config\systemprofile
    folder
    3. Grant NETWORK_SERVICE account full permission to the folder tree: : C:\Windows\SysWOW64\config\systemprofile

    When I ran my web service in the debug mode, the application go pass Excel worksheet statement excel.open() but I donot see the Excel application launch at all ( i have set the Excel visible to true) and it stuck at the Run Macro statement. I looked at the task manager, I do not see any Excel instance being create at all. I am not sure what causes the problem. (Note: the application run fine is the IDE with integrated web server)
    Below is my sample code:

    Imports Microsoft.Office.Interop.Excel
    Dim oExcel As Application = New Application

    Dim oBook As Workbook
    Dim oBooks As Workbooks
    Try
    ‘Start Excel and open the workbook.
    ‘oExcel = CreateObject(“Excel.Application”)
    oExcel.Visible = True
    oExcel.DisplayAlerts() = True
    oBooks = oExcel.Workbooks
    oBook = oBooks.Open(“c:\temp\book1.xls”)

    ‘Run the macros.
    oExcel.Run(“DoKbTest”)

    ‘Clean-up: Close the workbook and quit Excel.
    oBook.Close(False)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oBook)
    oBook = Nothing
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oBooks)
    oBooks = Nothing
    oExcel.Quit()
    System.Runtime.InteropServices.Marshal.ReleaseComObject(oExcel)
    oExcel = Nothing

    GC.Collect()

    returnStr = “execute sucessfully”
    Catch ex As Exception
    oExcel.Quit()
    oExcel = Nothing
    GC.Collect()

    returnStr = “Exception occurred: ” & ex.Message

    ‘System.Console.WriteLine(“Error occurred ” & ex.Message)
    End Try

    Return returnStr

    I ‘d appreciate all your help.

    Chi

    1. Malte

      Hi Chi,

      Are you sure that the macro works? Can you run it manually?
      Perhaps you could try doing something else to test the interop – like creating or editing a wokbook. If that works at least you’ve narrowed it down to the macro statement.

      As for Excel not appearing in task manager, that is very odd (make sure to click ‘Show processes from all users’). And you’re not getting any exceptions? Perhaps check the event log for any messages that may have been missed.

    2. Kushan

      Hi Chi,
      I have same problem but its for word. my iis application stopped working and i have to reset iis. how you got it resolved?

      thanks in advance.

      Kushan

  10. chi

    Hi Malte,

    yes, the Macro is working. It is simple macro that I display the simple message to confirm the macro function is executed. I am able to run my web service inside the IDE with integrated web server without problem. The application launch the Excel application, open the workbook, run the macro to display the testing message and close the Excel instance correctly.

    when running the web service from IIS server, the application is hang when try to executing the run statement to run the macro function. The browser status symbol keeps spinning to wait for response from web service, There is no error exceptions. I looked at the event viewer (sinde the browser is not response or return any message) but I do not see any related error. I could not find any strange thing happen inside the web logs neither.

    I will follow your suggestion to see if it works with create or edit the worksheet. I will update after the test.

    Thanks

    Chi

  11. chi

    Hi Malte,

    I tried to read the Excel spreadsheet but I can’t read the file at all. The web page just hang; no repsonse. I looked at the task manager; there is an Excel application run under application pool account which I granted the permission on Desktop folder and Dcom. I am not getting any error exception at all. The web page just keep waitng for reponse. I found nothing on the event log. The Macro statement and the loop through spreadsheet code is working fine within the visual Studio 2010.

    Thanks

    Chi

    1. Malte

      Hi Chi, that is very odd. Unfortunately there are many things which could be a factor, including the application pool identity, IIS settings, permissions, Excel version etc.

      As it works for you when you run it through VS I would suggest you look for the problem with IIS and the app. pool. Another thing I would try is to use something like procmon to find out what exactly that Excel process is getting up to and perhaps even see an exception that had not been surfaced before.

  12. Sergio

    Hi Malte.
    Great article. I have IIS7 windows 7 x64 and Office 2007. My app works under vstudio development server but not on IIS.

    I’ve got first error, but i can’t complete your procedure, case I don’t see Excel under DCOM Config node.

    1. Malte

      Hi Sergio, have you tried with mmc -32 under step 1?

      1. Sergio

        Yes. mmc -32 shows empty console

        1. Malte

          After mmc -32 you need to go to File > Add/Remove Snap-in and add the Component Services snap-in to the management console.

  13. chi

    Hi Malte,

    Right now, i have problem with the Excel now. I received “cannot use object linking or Embedding” error whenever I click on the Excel spreadsheet. 🙁

    I will try your suggest to use process monitor.

    Thanks for response.

    Chi

  14. Phil

    Finally someone who’s posted a proper fix for this issue.

    I don’t normally bother commenting but this has really helped me.

    i would like to add something though. Microsoft Excel Application never seems to be in the list in DCOM config for me. There are two CLSIDs at the bottom of the list that begin 00 (never the same as the error message says) which if you click properties show a path pointing to office.14. Add network service as above for both of these and it worked for me.

    Thanks again.

  15. Ted Porter

    This artical is a life save.

    I have just ported a mixed Classic ASP and ASP.NET web application from Server 2003 32-bit to Server 2008 R2 and nearly had to go back to the old platform.

    Thank you so very much for publishing it and making it so easy to follow.

  16. Cris

    Hello,

    I was wondering if anyone here knew how to display an Excel file through a website with “using Excel = Microsoft.Office.Interop.Excel;”.

    I have been able to populate & open the excel file for the user when I press CNTRL-F5, so that I could run it locally, but when I run that same code on my ii7 server for other people to login & use it, I cannot get the excel sheets to open up or populate.

    I’ve been searching, but, seeing this post, I really hope someone can help to shorten the time I spend searching for a solution.

    I get no error message. It just doesn’t open the excel files or do anything when I click the button to open up the excel files. But, it works locally.

    Thank you so much in advance..

    Cris

  17. luis

    thank you very much….
    elsewhere only focused advice to the dcom configuration …
    the problem of the windows folder no one knew about this …

    greetings master

    PD. sorry my english

  18. Soniya

    Awesome post! Thanks a lot ..

  19. Kiran

    Hi Malte,
    Great artical.. very appreciable..
    Fighting for the above second issue since couple of hours.
    Your solutions saves my time.

  20. Nathaniel

    Great Article,

    I am using this for a simple excel import/export, it works perfectly but after a few hours of this running.
    Any webpage using this feature will respond with _blank page, debugging returns no error.
    It appears to be a timeout issue with dcom, has anyone had this happen to them?
    It happens that if i log into the server and open excel manually this stops for a few more hours, then reappears.

  21. Francisco Villegas

    Hola,

    Muchas gracias por la ayuda, he estado 2 dias pegado en varios foros, pero aqui encontre la soluci├│n!!

    Gracias!!

    Saludos,
    Francisco

  22. ram

    Great post. The both solutions worked for me. I have Windows web server 2008 32 bit version. ASP.net 1.1 and MS Office 2003. once i applied the above solutions i am able to read the excel file and insert the data into SQL server

  23. TComer

    Great info! I found that my application was running under the default “ApplicationPoolIdentity” in IIS7. Changing this to the Network Service account after configuring the application in mmc resolved the issue for me. Thanks again.

  24. Vinit

    It works… Many Thanks!

  25. mat

    Been working on this crap for 4 days straight (even completley destroyed a OS) and this worked perfectly.

    Cheers

  26. anduin

    In IIS 7.5 you need to change the app pool identity to NETWORK SERVICE from APPPOOLIDENTITY then give permissions to the desktop folder and run all the other dcom permissions through that.

    4 weeks, and a fractured skull later.

  27. yogesh

    I have my web service run on IIS 7.5, windows 7 64 bit. .NET framework 4.0, Microsoft office 2010 (32 bit).
    I have done changes according to the solution 1.
    I changed the settings shown above and given permission to IIS_IUSRS user, but changes are not persisted.
    When I again open the properties/security tab..changes were lost.

  28. Pushpakumara Mahagamage

    Thanks Friend,

    You save me, I spent about a day to fix the issue, thanks again.

    Regards,

    Pushpe

  29. chinthana

    thanks for sharing such a valuable article.it saved lot of times of us.we build the application for sending fax via fax modem by attaching the ms word document.it worked well in ms server 2003 then we migrated to server 2008 then we faced a problem with it.in my self t i searched it out but i couldn’t slove the problem , my boss luckly got the point finally.
    well gotta by boss and thank for article

  30. Mark

    Hey man!
    Thanks for this!
    How on earth did you find the directory thing? Appreciate the tip!

  31. Nueng

    Thx very much for solution.It’s work!!

  32. Luis Sérgio

    sometimes, you create new application pool and cann’t solve it via DCOMCNFG.

    then there is another method to do:

    set the identity (Model Name) of application pool to “LocalSystem”

    I don’t know the root cause, but it could solve some problems.

  33. Alex

    Hi,

    I was running into both of these issues and was able to fix it with your advice but with one addition:

    In addition to adding permissions to the NETWORK SERVICE user for both the DCOM and Desktop folder, I had to add the same permissions to the “Users” group (for DCOM and the Desktop folder). I came accross that solution in another website. However, I am wondering why I needed to do that. Should not the permissions for NETWORK SERVICE be sufficient? I’m also wondering if adding permissions to Users is a security hole, especially for the Desktop folder persmissions?

    Note, I’ve done all this in my local development machine running IIS 7.5. I have yet to try it on the test or production servers (Server 2008).

    Thanks

  34. Altaf

    Thanks a lot. This works for me after hardwork of googling…!

  35. Ivan

    It’s working! Thanx/

  36. Carl Wyles

    IMPORTANT NOTE:

    As a last thing to do on setup or after updates. Make sure to run the office app AS the user you setup to config the service.

    The various Office products might popup a dialog that has to be answered / configured before it goes into normal edit mode (EULA screens etc) . I pulled my hair out for a day or do on this one.

    The symptom of this one is that you can see WINWORD(in this case) show up in task manager and 2min later it disappears. Then the system error log shows message like this:

    The server {000209FF-0000-0000-C000-000000000046} did not register with DCOM within the required timeout.

  37. L.v.Dycke

    Many thanks !!!

  38. owais

    Hi I am using outlook for sending an email appointment request but it send an error

    Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

    if i run the solution under Visual studio its working fine if i host in IIS then this error will come. I try every solution but none of them work.Please suggest any solution .

  39. Annu

    Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>