InteropServices.COMException (0x800A1066): сбой команды в Teamcity

У меня есть следующий код на С# 4.5. Он просто записывает простую Hello world в документ Word с помощью 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);
}

Без проблем работает под Windows 7 и Windows Server 2012 R2. Но когда я запускаю его под Teamcity, на той же Windows Server 2012 R2, я ожидаю следующие ошибки:

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

person Emmanuel Chaffraix    schedule 21.03.2014    source источник


Ответы (1)


Для решения этой проблемы :

  • Проверьте эту тему. т.е.: создайте папку Desktop под C:\Windows\SysWOW64\config\systemprofile или C:\Windows\System32\config\systemprofile (в зависимости от вашей ОС)
  • Проверьте конфигурацию офисного компонента с помощью dcomcnfg.exe и измените идентификатор в свойствах на The interactive user.

Остерегайтесь, названия компонентов отличаются...

  • Слово: Document Microsoft Word 97-2003
  • Excel: Microsoft Excel Application

Вы можете получить зарегистрированные компоненты с помощью regedit на HKCR\AppId. Все компоненты Office 2013 заканчиваются на 0000-0000-C000-000000000046

person Emmanuel Chaffraix    schedule 21.03.2014
comment
Хм, не делайте этого, взлом профиля системной учетной записи никогда не будет правильным решением. Правильно задокументируйте свой вопрос и покажите, что содержит переменная file. И исправьте свою проблему, используя полное имя пути. - person Hans Passant; 21.03.2014
comment
ТС работает не под системной учетной записью, у него свой пользователь. Переменная файла содержит: Path.Combine(Directory.GetCurrentDirectory(), "test.docx"); Я также пробовал с AppDomain.CurrentDomain.BaseDirectory - person Emmanuel Chaffraix; 24.03.2014