Microsoft Interop saveAs command failed

2019-02-27 08:41发布

问题:

I have this simple console app that converts Word documents to PDF using Microsoft Office Interop API. For some reason, this one document always fails and I have attached it and removed all extraneous content: click here

For some reason, it works fine when you open the document and perform the saveAs function in Word, but through code, it fails. I've tried SaveAs2, SaveAs and ExportAsFixedFormat methods. I'm running Office 2010 and using Microsoft Word 14.0 Object Library. Thanks.

My C# code is as follows

        Object missing = Type.Missing;
        String file = @"C:\Test\bad2.doc";
        Word.Application wordApp = null;
        Word.Document document = null;
        try
        {
            wordApp = new Word.Application();
            wordApp.Visible = false;
            document = wordApp.Documents.Open(file);

            document.SaveAs2(@"C:\Test\bad.pdf", Word.WdSaveFormat.wdFormatPDF);
        }
        catch (Exception ex)
        {
            Console.Write(ex);
        }
        finally 
        {
            if (document != null) 
            {
                ((Word._Document)document).Close();
            }
            if (wordApp != null)
            {
                ((Word._Application)wordApp).Quit(ref missing, ref missing, ref missing);
            }
            document = null;
            wordApp = null;
        }