title: .net application failing when fired via scheduled task
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>The actual stack trace error which is only being generated when run through the scheduled task is:</p>
  
  <blockquote>
    <p>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)).</p>
  </blockquote>
  
  <p>The line of code which is doing this is:</p>
  
  <pre><code>Application oWord = new Application();
  </code></pre>
  
  <p>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.</p>
  
  <p>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??</p>
  
  <p>Adding - Based on @Dmitry Martovoi post below I have added ticks for the EVERYONE user and the stack trace has changed to:</p>
  
  <blockquote>
    <p>System.NullReferenceException: Object reference not set to an instance of an object.</p>
  </blockquote>
  
  <p>which is pointing to this line inside the same saveAs function:</p>
  
  <pre><code>oDoc.Close(false, Type.Missing, Type.Missing);
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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:</p>
  
  <p>1) Start->Run->dcomcnfg</p>
  
  <p>2) Component Services->Computers->My Computer</p>
  
  <p>3) Right-click My Computer, Properties</p>
  
  <p>4) COM Security tab, Launch and Activation Permissions, Edit Default</p>
  
  <p>5) Add all access rights for NETWORK SERVICE (which is the best account to use apparently), you are using to run scheduled task</p>
  
  <p>6) Right-click My Computer > DCOM Config > Microsoft Word 97 - 2003 Document > Properties</p>
  
  <p>7) Go to identity Tab and tick the interactive user </p>
  
  <p>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.</p>
  
  <p>9) Repeat from step 6 for other office document types.  I have done so for xls and mdb</p>
  
  <p>10) restart....All are working well now</p>
  
  <p>Sorry I should have added this.  If you can not see these at step 7:</p>
  
  <pre><code>               'Microsoft Word 97 - 2003 Document'
  
                 'Microsoft Excel Application'
  
                 'Microsoft Access Application
  </code></pre>
  
  <p>Then it probably a 32bit office installation on a 64 bit machine, here is <a href="http://blogs.technet.com/b/the_microsoft_excel_support_team_blog/archive/2012/11/12/microsoft-excel-does-not-appear-in-dcom-configuration-snap-in.aspx" rel="nofollow noreferrer">the  solution</a> to make them visible:</p>
  
  <p>Thank you everyone for your time</p>