奇怪的错误。 8007007E:正在检索组件的 COM class 工厂{} - 8007007E

Weird Error. 8007007E : Retrieving the COM class factory for component{} - 8007007E

我已经在 Windows 2003 服务器 32 位 OS 上部署了一个 Web 应用程序(在 VS 2008 中开发)。 我的应用程序使用 Microsoft Office Word(我用它来生成 PDF)。 应用程序 运行 非常好,正在生成 .Doc 和 .Pdf。 但是,一旦我重新启动服务器,我仅第一次收到上述错误 (8007007E),刷新页面后,应用程序再次开始正常工作。 我查找了导致错误的各种原因,但其中 none 个与我的相符。 通常,当发生此错误时,它会导致 COM 使用完全崩溃。 代码示例如下。

string htmlContent = div_Data.InnerHtml;
string filename = strLoanNo + strTime + ".doc";
if (!Directory.Exists(Server.MapPath("~/Doc/")))
{
 Directory.CreateDirectory(Server.MapPath("~/Doc/"));
}
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
div_Data.RenderControl(hw);
FileStream fs = new FileStream(Server.MapPath("~/Doc/") + filename,FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write(tw.ToString());
sw.Flush();
sw.Close();
fs.Close();

object fileName = Server.MapPath("~/Doc/") + filename;
object missing = System.Reflection.Missing.Value;
object readOnly = false;
object isVisible = true;
object SaveChanges = true;
Microsoft.Office.Interop.Word.ApplicationClass appWord = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document oWordDoc = appWord.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing, ref missing);
oWordDoc.ExportAsFixedFormat(Server.MapPath("~/Doc/") + strLoanNo + "_" + strTime + ".pdf", WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 1, 1, WdExportItem.wdExportDocumentWithMarkup, true, false, WdExportCreateBookmarks.wdExportCreateNoBookmarks, false, true, true, ref missing);
((_Document)oWordDoc).Close(ref SaveChanges, ref missing, ref missing);
 appWord.Quit(ref SaveChanges, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(appWord);

Response.Clear();
Response.Charset = "";
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + strLoanNo + "_" + strTime + ".pdf");
        Response.WriteFile(Server.MapPath("~/Doc/") + strLoanNo + "_" + strTime + ".pdf");
Response.Flush();
Response.Close();

if (File.Exists(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".doc"))
{
  File.Delete(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".doc");
}
if (File.Exists(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".pdf"))
{
  File.Delete(Server.MapPath("~/Doc/") + strLoanNo + strTime + ".pdf");
}

不确定此时代码是否重要,因为问题可能出在某处的 DLL 上,但是只有系统管理员才能告诉您哪个正在努力。如果可以,请使用进程监视器来隔离发生故障的组件 https://technet.microsoft.com/en-us/sysinternals/bb896645.aspx

谢谢, 顶点