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

C# 如何解决错误:由于以下错误而失败:80040154类未注册,c#,asp.net,exception,com,ms-word,C#,Asp.net,Exception,Com,Ms Word,如何解决下面的错误。 此错误在运行时获取 正在检索具有CLSID的组件的COM类工厂 {000209FF-0000-0000-C000-0000000000 46}由于以下原因失败 错误:80040154类未注册(HRESULT异常: 0x80040154(REGDB_E_CLASSNOTREG)) 代码: 此代码用于将word转换为pdf文档文件。 我在这一行遇到了错误 Application wordApp = new Microsoft.Office.Interop.Word.Applic

如何解决下面的错误。 此错误在运行时获取

正在检索具有CLSID的组件的COM类工厂 {000209FF-0000-0000-C000-0000000000 46}由于以下原因失败 错误:80040154类未注册(HRESULT异常: 0x80040154(REGDB_E_CLASSNOTREG))

代码: 此代码用于将word转换为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;
        }
    }

服务或web应用(如IIS)中不应使用Office应用。其次,interop.word.dll类似于头文件,您实际上需要安装Office\word才能使用它

请注意微软在这方面的立场:

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

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

看起来您正在进行Office的服务器端自动化,这是一个禁忌,原因如下:


在服务器端,使用OpenXML或更好的ClosedXML并将其作为docx文件处理更可靠(也更受支持)。既然您正在转换为PDF格式,我建议您检查一下这个…

这太普通了,您能举个例子,说明您是从哪里得到这个版本的吗?这是word的正确版本吗?您在哪里运行它您需要与interop版本或更新版本相同的版本。Microsoft不建议在服务器环境中使用office interop-是吗?我的系统没有安装office。如果没有安装office,则完全可能会出现错误。Downvoter,是否要解释?Microsoft KB文章解释了它不受支持。解决方案:在运行该程序的实例中安装Microsoft Office。但这只是在你确信你想违背微软的建议的情况下。