如何解决错误:由于以下错误而失败:80040154类未注册

时间:2015-06-17 09:36:57

标签: c# asp.net exception com ms-word

如何解决以下错误。 运行时出现此错误。

  

使用CLSID检索组件的COM类工厂   {000209FF-0000-0000-C000-000000000046}由于以下原因而失败   错误:80040154未注册类(HRESULT异常:   0x80040154(REGDB_E_CLASSNOTREG))。

代码: 此代码用于将单词转换为pdf文档文件。 我在这一行得到了错误。

Application wordApp = new Microsoft.Office.Interop.Word.Application();
Document wordDocument = new Document();           
private void ConvertWord2PDF(string inputFile, string outputPath)
{

        try
        {
            if (outputPath.Equals("") || !File.Exists(inputFile))
            {
                throw new Exception("Either file does not exist or invalid output path");
            }

            // app to open the document belower
            Application wordApp = new Microsoft.Office.Interop.Word.Application();
            Document wordDocument = new Document();

            // input variables
            object objInputFile = inputFile;
            object missParam = Type.Missing;

            wordDocument = wordApp.Documents.Open(ref objInputFile, ref missParam, ref missParam, ref missParam,
                ref missParam, ref missParam, ref missParam, ref missParam, ref missParam, ref missParam,
                ref missParam, ref missParam, ref missParam, ref missParam, ref missParam, ref missParam);

            if (wordDocument != null)
            {
                // make the convertion
                wordDocument.ExportAsFixedFormat(outputPath, WdExportFormat.wdExportFormatPDF, false,
                    WdExportOptimizeFor.wdExportOptimizeForOnScreen, WdExportRange.wdExportAllDocument,
                    0, 0, WdExportItem.wdExportDocumentContent, true, true,
                    WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missParam);
            }

            // close document and quit application
            wordDocument.Close();
            wordApp.Quit();

            Response.Write("File successfully converted");
            //ClearTextBoxes();
        }
        catch (Exception e)
        {
            throw e;
        }
    }

2 个答案:

答案 0 :(得分:2)

不应在服务或网络应用中使用Office应用,例如IIS。其次,interop.word.dll就像一个头文件,你实际上需要安装Office \ word才能使用它。

请注意微软对此的立场:

  

Microsoft目前不建议也不支持从任何无人参与的非交互式客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT服务)自动化Microsoft Office应用程序,因为Office可能会出现不稳定Office在此环境中运行时的行为和/或死锁。

Preview and convert Word files in ASP.Net -using OpenXML

答案 1 :(得分:0)

  

由于以下错误,检索CLSID为{000209FF-0000-0000-C000-000000000046}的组件的COM类工厂失败:80040154未注册类(HRESULT异常:0x80040154(REGDB_E_CLASSNOTREG))。

看起来你正在进行Office的服务器端自动化,这是 a no-no ,原因如下:

Considerations for server-side Automation of Office

在服务器端,它更可靠(并且支持)使用OpenXML或更好的ClosedXML,并将它们作为docx文件处理。由于您要转换为PDF,我建议您查看此QA ...