Search code examples
asp.netwindowsiis-6com+dcom

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


I have deployed a Web Application(developed in VS 2008) on Windows 2003 Server 32bit OS. My application uses Microsoft Office Word(i am using it to Generate PDF). Application is running perfectly fine, .Doc & .Pdf are being generated. But once i restart the server i get the above mentioned error (8007007E) only 1st time, after i refresh the page, the application starts to work properly again. i looked up various causes of the error, but none of them match mine. usually this when this error occurs it causes complete breakdown of COM usage. code sample is below.

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");
}

Solution

  • Not sure if code matters at this point, because the issue is probably with a DLL somewhere, however only the system admin will be able to tell you which one is struggling. If you can, use Process monitor to isolate what component is failing https://technet.microsoft.com/en-us/sysinternals/bb896645.aspx.

    Thanks, Apex