【问题标题】:Getting System.Runtime.InteropServices.COMException: Command failed获取 System.Runtime.InteropServices.COMException:命令失败
【发布时间】:2017-07-22 14:56:38
【问题描述】:

我使用 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;
        }

我有什么遗漏的吗?

【问题讨论】:

  • 服务器是否安装了同一个office版本?
  • 谷歌“word saveas error 4198”,点击率很高。 Fwiw,永远不要在服务器上运行 Office 程序,机器会着急。
  • @Stefan:是的,两者都有相同的 2007 版本

标签: c# com ms-word office-interop


【解决方案1】:

我遇到了与您提到的相同的错误。

检查服务器上安装的office是否在“另存为”选项中有pdf类型?

如果不可用,请从以下链接安装 2007 Microsoft Office Add-in: Microsoft Save as PDF or XPS

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

我希望它能解决错误。

【讨论】:

    猜你喜欢
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多