简体   繁体   中英

C# console app that does Excel Interop - failing when running as scheduled Task -System.UnauthorizedAccessException

As the title states, I have a C# console app which uses interop to open Excel and create a new workbook. The code works fine when running the console app via command line. However this exception is thrown when running the console app via a scheduled task:

System.UnauthorizedAccessException: Retrieving the COM class factory for component   with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005

It is thrown from the following call:

_xlApp = new Excel.Application()

The scheduled task is setup to use my credentials (I am an administrator). Based on other forums I have made sure I have granted full control to my account at Component Services --> Computers --> My Computer --> DCom Config --> Microsoft Excel Application, but no luck.

I'm on Windows 7 Enterprise 64 bit. Not sure what the next step should be, any help is appreciated

The error 80070005 is a COM Access Denied error. Are you sure that your credentials have the ability to instantiate the Interop Library? Check this link and follow some of the debug steps.
( I know you said you did the DCOMConfig thing already, but there are more test scenarios in this link and hopefully something here will help you )

I ended up writing a windows service to call out to a library containing the Excel generation code. That fixed the error. However there was another COM exception when calling the workbook.Save() method. No matter what I tried that error would not go away. I read another post which stated that this was a security precaution and therefore by design.

However, calling workbook. SaveAs() will produce the same result and works fine when called from a windows service.

Thanks for the input funkymushroom. Hopefully this post will be helpful to someone else struggling with Excel Interop automation.

I had the similar issue, I have resolved the issue by performing the following steps.

DCOM Configuration

  1. Click Start -> Run
  2. Enter DCOMCNFG and press OK. This will open the DCOMCNFG window.
  3. Browse down the tree to Console Root -> Component Services -> Computers -> My Computer
  4. right-click on "My Computer" and select properties
  5. Select the "Default Properties" tab
    • Enable Distributed COM on this computer - Option is checked
    • Default Authentication Level - Set to Connect
    • Default Impersonation Level - Set to Identify
  6. Select the "COM Security" tab
  7. Click on Access Permissions ' Edit Default a. Add "Anonymous", "Everyone", "Interactive", "Network", "System" with Local and Remote access permissions set.
  8. Click on Launch and Activation Permissions ' Edit Default a. Add "Anonymous", "Everyone", "Interactive", "Network", "System" with Local and Remote access permissions set.
  9. Click on OK
  10. Close the DCOMCNFG window

Later I got an exception while opening the Excel. So please make sure that the following paths are available on the server.

  • C:\\Windows\\SysWOW64\\config\\systemprofile\\Desktop
  • C:\\Windows\\SysWOW64\\config\\systemprofile\\AppData\\Roaming\\Microsoft
  • C:\\Windows\\SysWOW64\\config\\systemprofile\\AppData\\Local\\Microsoft

This might help guys like me.

I also had this problem - it turned out that on the scheduled task I needed to tick the box "Run with highest privileges" on the General tab of the task set-up. This resolved the problem - it was so simple! Hope it helps someone else too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM