C # console application that makes Excel Interop - crash when scheduled launch Task -System.UnauthorizedAccessException

As the title says, I have a C # console application that uses interop to open Excel and create a new workbook. The code works fine when starting a console application through the command line. However, this exception occurs when starting a console application using 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 called from the following call:

 _xlApp = new Excel.Application() 

The scheduled task is configured to use my credentials (I am an administrator). Based on other forums, I made sure that I had full control over my account in Component Services β†’ Computers β†’ My Computer β†’ DCom Config β†’ Microsoft Excel, but no luck.

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

+4
source share
4 answers

I ended up writing a Windows service to call a library containing Excel generation code. This fixed the bug. However, when calling the workbook.Save () method, another COM exception occurred. No matter what I tried, this error will not go away. I read another post that says it was a safety precaution and therefore in design.

However, calling a workbook SaveAs () will give the same result and works great when called from a Windows service.

Thanks for entering the funkymushroom. Hope this post is helpful for someone struggling with Excel Interop automation.

+2
source

Error 80070005 - this is an Error Accessing COM . Are you sure your credentials have the ability to instantiate the Interop library? Check the link and follow some debugging steps.
(I know that you already said that you already did DCOMConfig, but there are more test scripts in this link and hopefully something helps here)

+2
source

I had a similar problem, I solved the problem by following these steps.

DCOM configuration

  • Click "Start" β†’ "Run"
  • Type DCOMCNFG and click OK. This will open the DCOMCNFG window.
  • Browse down the tree to the root of the console β†’ Component Services β†’ Computers β†’ My Computer
  • right click on β€œMy Computer” and select properties
  • Select the "Default Properties" tab.
    • Enable distributed COM on this computer - option checked
    • Default Authentication Level - Set to Connect
    • The default impersonation level is set to identify
  • Select the COM Security tab
  • Click "Access Permissions" a. Add "Anonymous", "Everyone", "Interactive", "Network", "System" with the local and remote access permissions set.
  • Click "Launch and Activation Permissions". a. Add "Anonymous", "Everyone", "Interactive", "Network", "System" with the local and remote access permissions set.
  • Click OK
  • Close the DCOMCNFG window

Later I got an exception when opening Excel. Therefore, 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

It can help guys like me.

0
source

I also had this problem - it turned out that for the scheduled task, I needed to check the "Run with the highest privileges" checkbox on the "General" tab of the task settings. It solved the problem - it was so easy! Hope this helps someone else too.

0
source

All Articles