title: Exceptions running unit tests against Excel under Team Build 2010
body: <p>I have an Excel wrapper class using the Office PIAs.  I also have a limited test suite that was previously written to run on NUnit.  We are migrating to TFS2010 at work so I'm migrating the NUnit test overs to MSTest as well.</p>
  
  <p>The test suite runs fine on my dev machine and if executed manually with MSTest command line utility on the machine that runs the build agent.  However, when executed through Team Build all the tests that have anything to do with disk I/O (Open, Save, etc) fail.  My build agent is running on a domain account and that domain account is also a local admin on the same machine.  The few tests that do not do any disk I/O run fine, so I know Excel is being fired and available.  Just seems like a permissions issue or a limitation of the Team Build process.</p>
  
  <p>So here's an example function.  This is what makes me think it's an Excel I/O issue.  The File.Exists check passes fine.  I'm not receiving a FileNotFoundException in my test run, instead I'm receiving a COMException directly from the interop layer.</p>
  
  <pre><code>public void OpenXLS(string workbookFilePath) 
  {
     // Make sure given file path exists
     if (!File.Exists(workbookFilePath))
     {
        throw new FileNotFoundException(String.Format(CultureInfo.CurrentCulture,
           "File '{0}' cannot be found.", workbookFilePath));
     }
  
     // Open the Workbook
     _xlsWorkbook = _xlsWorkbooks.Open(workbookFilePath, 0, false, Missing.Value,
        "", Missing.Value, true, Missing.Value, Missing.Value, true, false,
        Missing.Value, Missing.Value, Missing.Value, Missing.Value);
  }
  </code></pre>
  
  <p>The Exception:</p>
  
  <pre><code>System.Runtime.InteropServices.COMException: Microsoft Excel cannot access the file 
  'C:\BuildPath\TestResults\TestRun\Out\TestBook.xls'. There are several possible reasons:
  
  • The file name or path does not exist.
  • The file is being used by another program.
  • The workbook you are trying to save has the same name as a currently open 
  </code></pre>