0

I tried almost everything that I could find here on StackOverflow but my code keeps throwing the following error:

Exception from HRESULT: 0x800A03EC

on the line with delete(). I was hoping you people could help me out.

Here's my current code

var xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook book =
xlApp.Workbooks.Open(File_name);
xlApp.DisplayAlerts = false;
Excel.Worksheet worksheet = (Excel.Worksheet)book.Worksheets[2];
worksheet.Delete();
book.Worksheets.Add();
xlApp.DisplayAlerts = true;
book.Save();
book.Close();
xlApp.Quit();
Marshal.ReleaseComObject(worksheet);
Marshal.ReleaseComObject(book);
Marshal.ReleaseComObject(xlApp);

And here's the other code i tried:

oXL.DisplayAlerts = false;
worksheet = (Excel.Worksheet)theWorkbook.Sheets[i];
((Excel.Worksheet)theWorkbook.Sheets[i]).Delete();
oXL.DisplayAlerts = true;
oWB.Save();
oWB.Close(false, missing, missing);
oSheet = null;
oWB = null;
oXL.Quit();

And some more variations

Microsoft.Office.Interop.Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook oWB;
Excel.Worksheet oSheet;
Excel.Workbooks oMWB;

and i'm using this reference:

using Excel = Microsoft.Office.Interop.Excel;
Mo Patel
  • 2,321
  • 4
  • 22
  • 37
Gilmoo
  • 50
  • 1
  • 6
  • Exception from HRESULT: 0x800A03EC – Gilmoo Jul 10 '14 at 13:46
  • What line gives you the error? Doing the actual delete? Have you debugged and made sure that all the objects are initialized and what you expect? Are you sure the sheet exists? Also, sometimes there are hidden sheets, like ones that hold the print_area. It's possible by referencing the sheets via index you're actually trying to delete a special sheet you can't get rid of. What's the name of the sheet at index 2? – Maurice Reeves Jul 10 '14 at 13:49
  • in the first example this line: worksheet.Delete(); in the second: ((Excel.Worksheet)theWorkbook.Sheets[i]).Delete(); – Gilmoo Jul 10 '14 at 13:51
  • I updated my comment. Please see my additional questions. – Maurice Reeves Jul 10 '14 at 13:53
  • i debugged it multiple times and the objects are initialized, and the sheet really do exist. The sheets are also not hidden. The name of the sheet is "sheet2" – Gilmoo Jul 10 '14 at 13:53
  • It's not a matter of hidden. There are special sheets that Excel uses that you're able to reference via COM Interop. What's the name of the sheet at index 2? Is it the sheet you expect? – Maurice Reeves Jul 10 '14 at 13:54
  • Try next: Go to Excel Options > Save > Save Files in this format > Select "Excel Workbook(*.xlsx)". – Yuliia Ashomok Jul 10 '14 at 13:54
  • yes it is the sheet i expected – Gilmoo Jul 10 '14 at 13:55
  • it already is a .xlsx file – Gilmoo Jul 10 '14 at 13:56
  • One thing I note from this [example](http://stackoverflow.com/questions/6839708/unable-to-delete-sheet-from-excel-and-it-even-gives-no-error-using-vb-net) that I don't see in yours is you're not activating the sheet before you try to delete it. Have you tried that as well? – Maurice Reeves Jul 10 '14 at 14:06

5 Answers5

1

Working with the Excel Interop libraries, I encountered this error many times. The main cause of this problem (a generic COM exception), most of the times, is that Excel tries to find something you've asked for, but Excel isn't able to find it. See this answer, it helped me a lot.

Reading these lines:

Excel.Worksheet worksheet = (Excel.Worksheet)book.Worksheets[2];
worksheet.Delete();

I think that you're trying to delete a worksheet that's not existing. Check your Excel document.

Community
  • 1
  • 1
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
1

I just made the most stupid mistake ever..... the excel file was a shared file, that's why i couldn't delete it..

Sorry for making such a stupid mistake, and thanks to everybody who tried to help me!

Gilmoo
  • 50
  • 1
  • 6
  • haha I have had this happen once myself... consider flagging your question for moderators attention and trying to get it deleted as it is unlikely to ever help a future visitor :p –  Jul 11 '14 at 07:40
0

HRESULT: 0x800A03EC is an unknown COM error. This usually happens when Excel throws some error because your input or parameters were wrong.

This example provide msdn: Programmatically Deleting Worksheets from Workbooks

((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[4]).Delete();

So try next:

Excel.Worksheet worksheet = (Excel.Worksheet)book.Sheets[2];
worksheet.Delete();

instead of:

Excel.Worksheet worksheet = (Excel.Worksheet)book.Worksheets[2];
worksheet.Delete();
Yuliia Ashomok
  • 8,336
  • 2
  • 60
  • 69
0

Try this:

xlApp.DisplayAlerts = false;
Excel.Worksheet worksheet = (Excel.Worksheet)book.Worksheets[2];
worksheet.Delete();
xlApp.DisplayAlerts = true;

Also important to keep in mind, interop starts to count from 1, not from 0. so deleting item [0] or deleting the only sheet will throw you an exception. if you plan to remove the [2] worksheet, the third one will take its place. So make sure to remove from the last to the first.

etr
  • 1,252
  • 2
  • 8
  • 15
0

this is code i have used to delete the excel sheet

 Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

                if (xlApp == null)
                {
                    return;
                }

                xlApp.DisplayAlerts = false;
                string filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
                                        + "\\Sample.xlsx";
                Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(filePath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
                Excel.Sheets worksheets = xlWorkBook.Worksheets;

                worksheets[4].Delete();
                worksheets[3].Delete();
                xlWorkBook.Save();
                xlWorkBook.Close();

                releaseObject(worksheets);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);

and use this

static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                throw ex;

            }
            finally
            {
                GC.Collect();
            }
        }
Vinoth
  • 972
  • 1
  • 16
  • 47