-1

I try to open an Excel file in C#, but I get this error:

System.Runtime.InteropServices.COMException HResult=0x800A03E

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using excel = Microsoft.Office.Interop.Excel;

namespace Example_Reading_A_File_From_Excel
{
    class Program
    {
        static void Main(string[] args)
        {
            excel.Application x1app = new excel.Application();
            excel.Workbook x1workbook = x1app.Workbooks.Open(@"‪D:\saniaertest.xlsx");
            excel._Worksheet x1worksheet = x1workbook.Sheets[1];
            excel.Range x1range = x1worksheet.UsedRange;

            string website;

            for(int i = 1; i <= 3; i++)
            {
                website = x1range.Cells[i][9].value2;

                IWebDriver driver = new ChromeDriver();
                driver.Navigate().GoToUrl(website);
                Thread.Sleep(30);
                driver.Close();
            }
        }
    }
}

Normally it should open the Excel file and open the Chrome browser and navigate to all the URL that are listed in column 9. But like said before I only get the error:

System.Runtime.InteropServices.COMException HResult=0x800A03E

It says that the workbook can't be found.

oherby
  • 1
  • 2
  • Just the exception type is not enough. Provide the full exception message and the corresponding HRESULT value. – dymanoid Aug 09 '19 at 11:28
  • You never quit the `excel.Application x1app`. Also see [here](https://stackoverflow.com/questions/15728676/proper-way-of-releasing-com-objects) – Theo Aug 09 '19 at 11:28
  • System.Runtime.InteropServices.COMException HResult=0x800A03EC – oherby Aug 09 '19 at 12:04
  • @oherby What you just put in chat differs from what you have in your question. Please edit your question and clarify. – JeffC Aug 09 '19 at 12:43
  • Perhaps the workbook can't be found... because that's what the error message stated. Did you confirm that the workbook is in that path and that you have no spelling mistakes? Does the workbook end in .xls or .xlsx? etc. – JeffC Aug 09 '19 at 14:34

2 Answers2

0

That's very odd because I tried the first few lines of your code and I was getting the same error although my file did exist where I placed it.

For some reason, the Workbooks.Open is not pointing to the correct file path so I had to update it to the following:

string file = Directory.GetCurrentDirectory() + "\\" + "Test.xlsx";

if (File.Exists(file))
  {
     excel.Application x1app = new excel.Application();
     excel.Workbook x1workbook = x1app.Workbooks.Open(file);
     excel._Worksheet x1worksheet = x1workbook.Sheets[1];   
     excel.Range x1range = x1worksheet.UsedRange;
  }

This worked for me as I no longer get the error that the workbook cannot be found.

Gary Burch
  • 31
  • 2
0

I suggest using the ExcelDataReader package instead. It does not require having Excel or the InteropServices installed on a machine. As a bonus it also works to read files while running in Linux.

using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
    // Auto-detect format, supports:
    //  - Binary Excel files (2.0-2003 format; *.xls)
    //  - OpenXml Excel files (2007 format; *.xlsx)
    using (var reader = ExcelReaderFactory.CreateReader(stream))
    {
        // Choose one of either 1 or 2:

        // 1. Use the reader methods
        do
        {
            while (reader.Read())
            {
                // reader.GetDouble(0);
            }
        } while (reader.NextResult());

        // 2. Use the AsDataSet extension method
        var result = reader.AsDataSet();

        // The result of each spreadsheet is in result.Tables
    }
}

-Example from: https://github.com/ExcelDataReader/ExcelDataReader

J.D. Cain
  • 639
  • 4
  • 16