0

I'm writing a basic program to manipulate Excel using C#, and I'm having trouble assigning a range. Here's my code:

using Excel = Microsoft.Office.Interop.Excel;
//...various other using statements

static void Main(string[] args)
        {
            Excel.Application xlApp = new Excel.Application();
            xlApp.Visible = true;
            Excel.Workbook bk=xlApp.Workbooks.Add();
            Excel.Worksheet sht = bk.Sheets["Sheet1"];
            Excel.Range rng;
            for (int c = 0; c < 10;c++ )
            {

                rng=sht.Cells[c,1];
                rng.Value= "aaa";
            }

        }

I keep getting the following error on the rng=sht.Cells[c,1]; line:

An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Additional information: Exception from HRESULT: 0x800A03EC
sigil
  • 9,370
  • 40
  • 119
  • 199
  • 1
    try a google search on that Exception here is a SO previous posting that you can review as well http://stackoverflow.com/questions/891394/excel-error-hresult-0x800a03ec-while-trying-to-get-range-with-cells-name\ `Basically you are expecting Excel to find something that does not Exist` kind of the same as Index out of Bounds more or less... – MethodMan Oct 07 '14 at 17:06

2 Answers2

3

The first index to Cells will be 0 at the start of the look which XL does not like. Start from 1.

0

Solved it; the problem is that Excel uses a 1-based index, and my For loop started from 0.

sigil
  • 9,370
  • 40
  • 119
  • 199