'Invalid Index' using Excel Interop in C#
<p>I am getting an <strong>Invalid Index COM Exception</strong> when attempting to retrieve a page break item that should exist.</p>

<p>It appears every time I attempt to access the 3rd or higher item in an Excel.Worksheet.HPageBreaks list, it returns an invalid index error. If it's item 1 or 2, it has no problem retrieving the item and giving me the parameter I need. (I'm trying to retrieve the page break row on each page to make sure the data I am entering does not get separated at certain points.)</p>

<p>I have thrown in my existing code for how I am initializing this document because maybe there is some parameter I am setting that you see is conflicting, which I may not be aware of. My output is MessageBoxes.</p>

<p>Here is my existing code:</p>

<pre><code>        Excel.Application xApp = new Excel.Application();
        Excel.Workbook xWorkBook;
        Excel.Worksheet xWorkSheet;
        object missingVal = System.Reflection.Missing.Value;

        xWorkBook = xApp.Workbooks.Add(missingVal);
        xWorkSheet = xWorkBook.Worksheets.get_Item(1);
        xWorkSheet.PageSetup.Orientation =
Microsoft.Office.Interop.Excel.XlPageOrientation.xlPortrait;
        xWorkSheet.PageSetup.Zoom = false;
        xWorkSheet.PageSetup.FitToPagesTall = 20;
        xWorkSheet.PageSetup.FitToPagesWide = 1;
        xApp.UseSystemSeparators = false;

        xWorkSheet.Columns[1].ColumnWidth = 25;
        xWorkSheet.Columns[2].ColumnWidth = 10;
        for (int i = 3; i &lt; 28; i++)
            xWorkSheet.Columns[i].ColumnWidth = 1;

        var range = xWorkSheet.get_Range("A1", "AB1");
        range.Merge();
        range.Font.Size = 16;
        range.RowHeight = 21;
        range.Font.Bold = true;
        range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
        range = xWorkSheet.get_Range("A2", "AB2");
        range.Merge();
        range.Font.Size = 10;
        range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;

        MessageBox.Show(xWorkSheet.HPageBreaks.Count.ToString()); //This gives zero pagebreaks
        xWorkSheet.Cells[500, 1] = " ";
        MessageBox.Show(xWorkSheet.HPageBreaks.Count.ToString()); //This gives nine pagebreaks
        MessageBox.Show(xWorkSheet.HPageBreaks.Item[1].Location.Row.ToString()); //This returns 51
        MessageBox.Show(xWorkSheet.HPageBreaks.Item[2].Location.Row.ToString()); //This returns 102
        MessageBox.Show(xWorkSheet.HPageBreaks.Item[3].Location.Row.ToString()); //This is where it breaks with the error
</code></pre>

<p>At the bottom of the code, I verified before checking the items that there are 9 items total in pagebreaks using <code>xWorkSheet.HPageBreaks.Count.ToString()</code>. It just does not seem to be able to retrieve any items past item 2. Not sure if it matters, but I have tried switching</p>

<pre><code>xWorkSheet.HPageBreaks.Item[3]
</code></pre>

<p>to</p>

<pre><code>xWorkSheet.HPageBreaks[3]
</code></pre>

<p>and to</p>

<pre><code>xWorkSheet.HPageBreaks[xWorkSheet.HPageBreaks.Count] //Success at 1 or 2 PBs only
</code></pre>

<p>and tried different placements for the space character in the excel sheets other than row 500. The </p>

<p>I am absolutely out of ideas and have searched for solutions, only to find an irrelevant solution posted in 2010. This is my first question ever, so if I'm missing anything, please let me know! :)</p>

<p>Any ideas or a solution that I may have missed?</p>

<p>Thanks guys.</p>

<p>EDIT: Unfortunately, I also attempted using a foreach loop, just like <a href="https://stackoverflow.com/questions/19850543/how-to-know-the-page-break-in-excel-using-c-sharp">this post</a>, with it again returning an invalid index error specifically pointing to the foreach-loop as the cause. I'm running out of ideas...</p>