InteropServices.COMException (0x800A1066): Command failed under Teamcity

Emmanuel Chaffraix picture Emmanuel Chaffraix · Mar 21, 2014 · Viewed 7.7k times · Source

I have the following code in C# 4.5. It just writes a simple Hello world into a word document using Office 2013.

_Application word = new Application();
word.Visible = false;
_Document document = word.Documents.Add();
var file = Path.Combine(Directory.GetCurrentDirectory(), "test.docx");

try
{
    document.Words.First.InsertBefore("Hello World");
    document.SaveAs(file);
}
catch (Exception e)
{
    logger.Fatal(e);
}
finally
{
    if (document != null)
    {
        document.Close();
        Marshal.FinalReleaseComObject(document);
    }
    application.Quit();
    Marshal.FinalReleaseComObject(application);
}

It runs without any problems under Windows 7 and Windows Server 2012 R2. But when I launch it under Teamcity, on the same Windows Server 2012 R2, I expect the following errors :

System.Runtime.InteropServices.COMException (0x800A1066): Command failed
   at Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object& FileName, Object& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles, Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts, Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELetter, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Object& LineEnding, Object& AddBiDiMarks)
   at Interop.HelloWorld.HelloWord.<>c__DisplayClass4.<WriteIt>b__3() in c:\TeamCity\buildAgent\work\27b855ae6e536c44\Interop\HelloWorld\HelloWord.cs:line 58
   at Interop.HelloWorld.HelloWord.WithDocument(_Application application, _Document document, Action handler) in c:\TeamCity\buildAgent\work\27b855ae6e536c44\Interop\HelloWorld\HelloWord.cs:line 72

System.Runtime.InteropServices.COMException (0x800A1066): Command failed
  at Microsoft.Office.Interop.Word.DocumentClass.Close(Object& SaveChanges, Object& OriginalFormat, Object& RouteDocument)
  at Interop.HelloWorld.HelloWord.WithDocument(_Application application, _Document document, Action handler) in c:\TeamCity\buildAgent\work\27b855ae6e536c44\Interop\HelloWorld\HelloWord.cs:line 92
  at Interop.HelloWorld.HelloWord.WriteIt(String file) in c:\TeamCity\buildAgent\work\27b855ae6e536c44\Interop\HelloWorld\HelloWord.cs:line 52
  at Interop.Program.Main(String[] args) in c:\TeamCity\buildAgent\work\27b855ae6e536c44\Interop\Program.cs:line 25

Answer

Emmanuel Chaffraix picture Emmanuel Chaffraix · Mar 21, 2014

To solve this problem :

  • Check this thread. ie : Create Desktop folder under C:\Windows\SysWOW64\config\systemprofile or C:\Windows\System32\config\systemprofile (depending your OS)
  • Check the configuration of office component with dcomcnfg.exe and change the identity into the properties to The interactive user.

Beware components names differ...

  • Word : Document Microsoft Word 97-2003
  • Excel : Microsoft Excel Application

You can retrieve registered components with regedit on HKCR\AppId. All Office 2013 components finish by 0000-0000-C000-000000000046