Microsoft Interop saveAs command failed

五迷三道 提交于 2019-12-31 03:04:38

问题


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

来源:https://stackoverflow.com/questions/17383430/microsoft-interop-saveas-command-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!