mathieu_cupryk
asked on
Exception from HRESULT: 0x800A03EC. with excel program in C#
Exception Details: System.Runtime.InteropServ ices.COMEx ception: Exception from HRESULT: 0x800A03EC.
Source Error:
Line 32: xlApp.Visible = true;
Line 33: string strFileName = sFileName;
Line 34: Excel.Workbook xlWB = xlApp.Workbooks.Open("Exce lData.xls" ,
Line 35: null,
Line 36: null,
Source File: c:\inetpub\wwwroot\webappl ication1\w ebform2.as px.cs Line: 34
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls ;
using System.Diagnostics;
using System.Reflection;
using Excel= Microsoft.Office.Interop.E xcel;
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Runtime.InteropServ ices; // For COMException
namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected Excel.Application xlApp;
protected Excel.Workbook xlWB1;
private void searchExcelSpreadSheet(str ing sFileName)
{
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
string strFileName = sFileName;
Excel.Workbook xlWB = xlApp.Workbooks.Open("Exce lData.xls" ,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null);
Excel.Worksheet xls = (Excel.Worksheet)xlWB.Shee ts[1];
int intRows, intCols, r, c;
intRows = xls.UsedRange.Rows.Count;
intCols = xls.UsedRange.Columns.Coun t;
for(r = 1; r < intRows; r++)
{
for(c=1; c < intCols; c++)
{
string text = (string)((Excel.Range)xls. Cells[r,c] ).Text;
Response.Write (text+"<br>");
}
}
xlWB.Close(false, null, null);
xlApp.Quit();
xls = null;
xlWB = null;
xlApp = null;
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
searchExcelSpreadSheet ("\\Excel\\ExcelData.xls") ;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.P age_Load);
}
#endregion
}
}
Source Error:
Line 32: xlApp.Visible = true;
Line 33: string strFileName = sFileName;
Line 34: Excel.Workbook xlWB = xlApp.Workbooks.Open("Exce
Line 35: null,
Line 36: null,
Source File: c:\inetpub\wwwroot\webappl
Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls
using System.Diagnostics;
using System.Reflection;
using Excel= Microsoft.Office.Interop.E
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Runtime.InteropServ
namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected Excel.Application xlApp;
protected Excel.Workbook xlWB1;
private void searchExcelSpreadSheet(str
{
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
string strFileName = sFileName;
Excel.Workbook xlWB = xlApp.Workbooks.Open("Exce
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null);
Excel.Worksheet xls = (Excel.Worksheet)xlWB.Shee
int intRows, intCols, r, c;
intRows = xls.UsedRange.Rows.Count;
intCols = xls.UsedRange.Columns.Coun
for(r = 1; r < intRows; r++)
{
for(c=1; c < intCols; c++)
{
string text = (string)((Excel.Range)xls.
Response.Write (text+"<br>");
}
}
xlWB.Close(false, null, null);
xlApp.Quit();
xls = null;
xlWB = null;
xlApp = null;
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
searchExcelSpreadSheet ("\\Excel\\ExcelData.xls")
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.P
}
#endregion
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The link refered to in the accepted answer has died. Is there another way to view the accepted solution? Thank you!
The fix is in the first comment.
use the attached code snippet for line 34,
i.e. use System.Missing.Value instead of using NULL.
i.e. use System.Missing.Value instead of using NULL.
Excel.Workbook xlWB = xlApp.Workbooks.Open("ExcelData.xls",oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);
regards
prakash