MS Ofice Interop错误-检索具有CLSID ...的组件的COM类工厂HRESULT的异常:0x80070005E_ACCESSDENIED

问题描述

我知道这可能是一个重复的问题,但是我尝试了前面讨论的几乎所有步骤,但是没有运气。我也尝试了与我的情况类似的链接,并完成了所有说明,但仍然无法解决错误Retrieving the COM class factory for component with CLSID failed due to the following error: 80070005 Access is denied

在我的asp.net Web应用程序中,我需要打开Microsoft Word文件,我可以使用本地计算机中的程序集引用“ Microsoft.Office.Interop.Word”来执行此操作。但是,当我在服务器(Windows Server 2012 r2)上运行它时,我会遇到上述错误。我还在本地计算机上使用的服务器中安装了相同版本的MS Office。

下面是代码

 protected void Button1_Click(object sender,EventArgs e)
    {
        fileupload1.SaveAs(Server.MapPath("~/ControlPanel/DocumentCache/" + fileupload1.FileName));
        object filename = Server.MapPath("~/ControlPanel/DocumentCache/" + fileupload1.FileName);
        Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass();
        Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
        object readOnly = false;
        object isVisible = true;
        object missing = System.Reflection.Missing.Value;
        try
        {
            doc = AC.Documents.Open(ref filename,ref missing,ref readOnly,ref isVisible,ref missing);
            txt_DContent.Text = doc.Content.Text;
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.ToString();
        }
        
        INSERTDATAT(); // Method to insert the record
        // AC.Quit(); This is to quit MS word that was opened to read the ms word file in textBox txt_DContent
        ((_Application)AC.Application).Quit(false);
        Session["MyFile"] = fileupload1.FileName;
    }

enter image description here

解决方法

我已通过以下2个步骤修复了该错误:

  1. 在web.config文件的内部添加<identity impersonate="true" />

  2. 我已经在数据库中为NT AUTHORITY \ IUSR创建了一个安全登录名,并提供了所有必需的权限,如下所示:

enter image description here

enter image description here