Получение System.Runtime.InteropServices.COMException: сбой команды

Я использовал Microsoft.Office.Interop.Word для преобразования слова в pdf, который отлично работает на моей локальной машине.

Но когда я переместил исполняемые файлы на сервер (на сервере установлен Microsoft Office), он показывает мне исключение ниже.

Unhandled Exception: System.Runtime.InteropServices.COMException: Command failed

   at Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object& FileName, Objec
t& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles,
 Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts,
 Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELette
r, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Objec
t& LineEnding, Object& AddBiDiMarks)
   at PDF_Converter.Program.ConvertWordToPdf(String sInputFile, String sOutputFi
le) in D:\Work\HtmlToPDF_Converter\HTML_PDF_Converter\IMAGE_PDF_Converter\Progra
m.cs:line 89
   at PDF_Converter.Program.Main(String[] args) in D:\Work\HtmlToPDF_Converter\H
TML_PDF_Converter\IMAGE_PDF_Converter\Program.cs:line 30

Ниже мой код для преобразования.

private static void ConvertWordToPdf(string sInputFile, string sOutputFile)
        {

            // Create a new Microsoft Word application object
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            // C# doesn't have optional arguments so we'll need a dummy value
            object oMissing = System.Reflection.Missing.Value;

            word.Visible = false;
            word.ScreenUpdating = false;


            if (File.Exists(sInputFile))
            {

                FileInfo wordFile = new FileInfo(sInputFile);

                // Cast as Object for word Open method
                Object filename = (Object)wordFile.FullName;

                // Use the dummy value as a placeholder for optional arguments
                Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref filename, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                doc.Activate();

                object outputFileName = sOutputFile;
                object fileFormat = WdSaveFormat.wdFormatPDF;

                // Save document into PDF Format
                doc.SaveAs(ref outputFileName,
                    ref fileFormat, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                // Close the Word document, but leave the Word application open.
                // doc has to be cast to type _Document so that it will find the
                // correct Close method.                
                object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                doc = null;

            }

            // word has to be cast to type _Application so that it will find
            // the correct Quit method.
            ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
            word = null;
        }

Есть ли что-то, чего мне не хватает?


person testuser    schedule 09.10.2014    source источник
comment
На сервере установлена ​​та же версия офиса?   -  person Stefan    schedule 09.10.2014
comment
Google word saves ошибка 4198, много-много просмотров. Fwiw, никогда не запускайте программу Office на сервере, машина в спешке перевернется.   -  person Hans Passant    schedule 09.10.2014
comment
@Stefan: Да, у обоих одинаковая версия 2007 года.   -  person testuser    schedule 09.10.2014
comment
Связано: stackoverflow.com/a/25254605/1768303   -  person noseratio    schedule 10.10.2014


Ответы (1)


Я получал ту же ошибку, что и вы упомянули.

Проверьте, имеет ли офис, установленный на сервере, тип pdf в параметрах «Сохранить как»?

Установите 2007 Microsoft Office Add-in: Microsoft Save as PDF or XPS по ссылке ниже, если она недоступна.

http://www.microsoft.com/en-in/download/details.aspx?id=7

Я надеюсь, что это устранит ошибку.

person Parth Soni    schedule 10.10.2014