Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

Exception from HRESULT: 0x800A03EC. with excel program in C#

Exception Details: System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800A03EC.

Source Error:


Line 32:                   xlApp.Visible = true;
Line 33:                   string strFileName = sFileName;
Line 34:                   Excel.Workbook xlWB =  xlApp.Workbooks.Open("ExcelData.xls",
Line 35:                         null,
Line 36:                         null,
 

Source File: c:\inetpub\wwwroot\webapplication1\webform2.aspx.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.Excel;
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Runtime.InteropServices; // 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(string sFileName)
            {
                  Excel.Application xlApp = new Excel.Application();
                  xlApp.Visible = true;
                  string strFileName = sFileName;
                  Excel.Workbook xlWB =  xlApp.Workbooks.Open("ExcelData.xls",
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null,
                        null);
                  Excel.Worksheet xls = (Excel.Worksheet)xlWB.Sheets[1];

                  int intRows, intCols, r, c;

                  intRows = xls.UsedRange.Rows.Count;
                  intCols = xls.UsedRange.Columns.Count;

                  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.Page_Load);
            }
            #endregion
      }
}
Avatar of prakash_prk
prakash_prk
Flag of India image

Instead of null pass System.Reflection.Missing.Value to the workbooks.open function..

regards
prakash
ASKER CERTIFIED SOLUTION
Avatar of prakash_prk
prakash_prk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of surleysue
surleysue

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.

Excel.Workbook xlWB =  xlApp.Workbooks.Open("ExcelData.xls",oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);

Open in new window