将MS-word转换为PDF时出错 [英] Error while converting MS-word to PDF

查看:106
本文介绍了将MS-word转换为PDF时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误消息未处理COMException命令失败"如何解决?

I get an error "COMException was unhandled Command failed" How to resolve it?

private void ConvertToPDF(string filePath)
{
            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;

            // Get list of Word files in specified directory
            DirectoryInfo dirInfo = new DirectoryInfo(filePath);
            FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

            word.Visible = false;
            word.ScreenUpdating = false;

            foreach (FileInfo wordFile in wordFiles)
            {
                // Cast as Object for word Open method
                Object filename = (Object)wordFile.FullName;

                // Use the dummy value as a placeholder for optional arguments
                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 = wordFile.FullName.Replace(".doc", ".pdf");
                object fileFormat = WdSaveFormat.wdFormatPDF;

                try
                {
                    // 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);

                }
                catch (Exception e)
                {
                }


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

推荐答案

不太清楚从何处获得此错误. (哪条线?)
您确定使用的是Office 2007或更高版本吗?并非所有版本都支持pdf.

2007 Microsoft Office加载项:Microsoft另存为PDF
http://www.microsoft.com/downloads/en/details.aspx?FamilyID = f1fc413c-6d89-4f15-991b-63b07ba5f2e5& displaylang = en [
Not very clear where you get this error. (Which line?)
Are you sure you are using office 2007 or higher? Not all versions support pdf.

2007 Microsoft Office Add-in: Microsoft Save as PDF
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=f1fc413c-6d89-4f15-991b-63b07ba5f2e5&displaylang=en[^]

Good luck!


这篇关于将MS-word转换为PDF时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆