2014-10-09 191 views
0

我已經使用Microsoft.Office.Interop.Word將word轉換爲pdf,這在我的本地機器上正常工作。獲取System.Runtime.InteropServices.COMException:命令失敗

但是,當我將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; 
     } 

有什麼我失蹤了嗎?

+0

是否在服務器上安裝了相同的Office版本? – Stefan 2014-10-09 10:06:43

+0

谷歌「word saveas錯誤4198」,很多很多點擊。 Fwiw,永遠不要在服務器上運行Office程序,機器會匆匆而過。 – 2014-10-09 10:07:17

+0

@Stefan:是的,兩者的2007版本相同 – testuser 2014-10-09 10:49:40

回答

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

我希望這將解決該錯誤。