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

安稳与你 提交于 2019-12-04 10:14:21

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!