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

时间:2016-01-08 11:28:37

标签: asp.net windows iis-6 com+ dcom

我在Windows 2003 Server 32位操作系统上部署了一个Web应用程序(在VS 2008中开发)。 我的应用程序使用Microsoft Office Word(我用它来生成PDF)。 应用程序运行完美,.Doc&正在生成.pdf。 但是一旦我重新启动服务器,我第一次得到上面提到的错误(8007007E),刷新页面后,应用程序再次开始正常工作。 我查找了错误的各种原因,但没有一个与我的相符。 通常在发生此错误时会导致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");
}

1 个答案:

答案 0 :(得分:1)

不确定代码在这一点是否重要,因为问题可能是某个DLL的某个地方,但是只有系统管理员才能告诉你哪个代码正在挣扎。如果可以,请使用“进程监视器”来隔离哪个组件失败https://technet.microsoft.com/en-us/sysinternals/bb896645.aspx

谢谢, 顶点