views:

203

answers:

2

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

+2  A: 

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)

funkymushroom
I tried all the steps under the local security policies and dcom configuration, the same error is still thrown
cyrix86
I have permission to instantiate the Interop Library. I have run this app successfully via command line/explorer. It only fails under the scheduled task
cyrix86
Does the scheduled task run as a different user?
funkymushroom
No it runs as my account. I am the admin of the box
cyrix86
I am not sure then, you might want to run it as a service.see http://support.microsoft.com/kb/137890 for running a command line tool as a service. That might be an alternate approach.
funkymushroom
Thanks for the responses. With the above method, would you control timing for when the command line tool was run? The idea with the scheduled task is that I could set it to run at a certain time on a weekly basis. If there is not a way to get this to work as a scheduled task then it would seem that the better plan would be to encapsulate the console app's logic into a library and call out to it from a windows service that I could write from the ground up and control the timing of the code execution. But I don't want to go that route if this can be made to work as a scheduled task.
cyrix86
With a service, its less about the timing and more about establishing if the service could be run under your credentials.You could schedule this service to start at a certain time every week (although this is very hacky/inelegant).I hear you on not wanting to go the latter route, that would take a bunch of time, and add extra risk to it.Have you tried writing a powershell script or a vbs to instantiate the Excel interop?
funkymushroom
+1  A: 

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.

cyrix86