Error While opening PDFand Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

0

After i convert code from word to PDf if i input the same word document to convet to PDf the PDF File is damaged. Can you tel me whether this error is because of inputting same word PDf many times or other error.

 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;
    }

i am getting error. this is how i converted word to pdf and read Pages using itextsharp.

c#
.net
visual-studio
ms-word
asked on Stack Overflow Jul 2, 2013 by pdp • edited Mar 2, 2015 by Deduplicator

1 Answer

1

Try this code:

        string tEmpName= Path.GetTempFileName();
        File.Delete(tmpName.ToString()); 
        File.Delete(Filename);
        File.Move(tEmpName, Filename);
answered on Stack Overflow Jul 10, 2013 by Pragya • edited Jul 10, 2013 by Arun Chandran C

User contributions licensed under CC BY-SA 3.0