打开PDF和类型不匹配时出错。 (HRESULT异常:0x80020005(DISP_E_TYPEMISMATCH))

时间:2013-07-02 11:10:09

标签: c# .net visual-studio ms-word

我将代码从单词转换为PDf后,如果我输入相同的单词文档以转移到PDf,则PDF文件已损坏。你能告诉我这个错误是因为多次输入相同的词PDf还是其他错误。

 Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
            object _MissingValue = System.Reflection.Missing.Value;

              public void WordtoPdf_Input()
 {
string filename_doc=System.IO.Path.GetFileName(LblFleip.Text);
string wordFileName = LblFleip.Text;
      string pdfFileName=string.Empty;
     appWord.Visible = false;
     appWord.ScreenUpdating = false;

  // Cast as Object for word Open method
  object filename = (object)wordFileName;

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

   InputFilename = pdfFileName = Path.ChangeExtension(wordFileName, "pdf");
  object fileFormat = WdSaveFormat.wdFormatPDF;

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

  // 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 _MissingValue, ref _MissingValue);
  doc = null;

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

我收到错误。这就是我如何将word转换为pdf并使用itextsharp读取Pages。

1 个答案:

答案 0 :(得分:1)

试试这段代码:

        string tEmpName= Path.GetTempFileName();
        File.Delete(tmpName.ToString()); 
        File.Delete(Filename);
        File.Move(tEmpName, Filename);