I have a .net console application written in c# which does exactly what it should when run inside visual studio and when clicking the .exe file in the file system. It runs like a charm. BUT when I create a scheduled windows task either in my Windows 7 dev machine or the Windows 2008 R2 prod machine it fails to carry out the last step in the application. This step is to open a .doc in the background and covert it (simply running a save as) to a .docx.
The application is designed to visita network path, create a dir called ~Converted which it does not problem and then for each .doc it finds it opens in background, performs a save as and them saves a .docx version of the do in the ~Converted dir. I believe there is nothing wrong with the code since it builds and runs without issue outside of the scheduled task. I have explored every setting inside the task, made alterations and re-tested with no success. I am running it wit a user who is part of the administrators group, IMO permissions is not the issue. I have tried it with the NETWORK SERVICE account, same result.
The actual stack trace error which is only being generated when run through the scheduled task is:
System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
The line of code which is doing this is:
Application oWord = new Application();
I have search for hours on this and found nothing helpful. It seems like running the code via the task is causing a COM access issue when the code is opening/initializing the word doc. The task is being fun with full admim privileges, running with highest privileges and when the user is logged on or not. As I said all these setting have been played with, it has not made a difference.
It has got to the stage now where I will have to re-write this console application as a service If I cant solve this issue. Hopefully somebody can save me a full days extra work and help me with the issue??
Adding - Based on @Dmitry Martovoi post below I have added ticks for the EVERYONE user and the stack trace has changed to:
System.NullReferenceException: Object reference not set to an instance of an object.
which is pointing to this line inside the same saveAs function:
oDoc.Close(false, Type.Missing, Type.Missing);
Adding - I have been changing users who own this path that much im not sure which user was running it this was generated. here is an application event log erorr. NOTE - This only happens when running the application via task scheduler:
Faulting application name: WINWORD.EXE, version: 14.0.6129.5000, time stamp: 0x5082f340
Faulting module name: btmofficea.dll, version: 3.0.2.280, time stamp: 0x4cc57919
Exception code: 0xc0000005
Fault offset: 0x0000000000005e1f
Faulting process id: 0x2a6c
Faulting application start time: 0x01ce8bec64ccf85d
Faulting application path: C:\Program Files\Microsoft Office\Office14\WINWORD.EXE
Faulting module path: C:\Program Files\Motorola\Bluetooth\btmofficea.dll
Report Id: a393403d-f7df-11e2-b68c-74de2b9ca154
OK problem is fixed. As can be seen in the link @dmay pasted MS do not recommend running office applications unattended on the server side and says this is unsupported. However the solution was down the track @Dmitry was headed:
1) Start->Run->dcomcnfg
2) Component Services->Computers->My Computer
3) Right-click My Computer, Properties
4) COM Security tab, Launch and Activation Permissions, Edit Default
5) Add all access rights for NETWORK SERVICE (which is the best account to use apparently), you are using to run scheduled task
6) Right-click My Computer > DCOM Config > Microsoft Word 97 - 2003 Document > Properties
7) Go to identity Tab and tick the interactive user
8) Go to Security tab and check the customize settings for all three sections. Make sure NETWORK SERVICE shows and has options ticked. This should be as a result of step 5.
9) Repeat from step 6 for other office document types. I have done so for xls and mdb
10) restart....All are working well now
Sorry I should have added this. If you can not see these at step 7:
'Microsoft Word 97 - 2003 Document'
'Microsoft Excel Application'
'Microsoft Access Application
Then it probably a 32bit office installation on a 64 bit machine, here is the solution to make them visible:
Thank you everyone for your time