Получаване на System.Runtime.InteropServices.COMException: командата е неуспешна

Използвах Microsoft.Office.Interop.Word за конвертиране на word в pdf, което работи добре на локалната ми машина.

Но когато преместих exe файловете на сървър (сървърът има инсталиран 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 saveas грешка 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