Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

My code looks like this

C#
object misValue = System.Reflection.Missing.Value;
           Excel.Application xlApp = new Excel.Application();
           xlApp.SheetsInNewWorkbook = SCANLA.Run.BusDetails.Count() ;
           Excel.Workbook xlWorkBook = xlApp.Workbooks.Add();
           xlApp.StandardFont = Constants.FONT_NAME;
           xlApp.StandardFontSize = Constants.FONT_SIZE;
           int SheetNumber = 0;
           string SheetName = null;

           try
           {
               string path = Properties.Settings.Default.OutputDirPath + "\\" + Constants.GT_EXCEL_NAME;
               if ((string)cmbNoBuses.SelectedItem == "2")
                   SheetName = "Bus 1,Bus 2";
               if ((string)cmbNoBuses.SelectedItem == "3")
                   SheetName = "Bus 1,Bus 2,Bus 3";
               if ((string)cmbNoBuses.SelectedItem == "4")
                   SheetName = "Bus 1,Bus 2,Bus 3,Bus 4";


               if (File.Exists(path))
                   File.Delete(path);

                   Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(SheetNumber);

                   GenerateExcelSheets( xlWorkSheet, SheetName);
                   MessageBox.Show(Constants.GT_STATUS, Constants.GT_HEADING, MessageBoxButtons.OK, MessageBoxIcon.Information);


               xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
               xlWorkBook.Close(true, misValue, misValue);
               xlApp.Quit();
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }
           finally
           {
               ReleaseObject(xlWorkBook);
               ReleaseObject(xlApp);
           }


Exception:
C#
An unhandled exception of type 'System.Exception' occurred in SCANLA.exe

Additional information: Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))


I am getting exception while executing this code.

Can anyone helpme in solving this


Thanks
John
Posted
Comments
Sergey Alexandrovich Kryukov 3-Feb-14 11:34am    
In what line?
—SA
Ranjan.D 3-Feb-14 11:34am    
Put a breakpoint and debug the code. Try to see the count of (Excel.Worksheet)xlWorkBook.Worksheets. Note : Your error message clearly says Invalid index.
Philippe Mori 3-Feb-14 21:30pm    
Have you verified if worksheets are 0-based or 1-based?
Bernhard Hiller 4-Feb-14 2:43am    
} catch (Exception ex)
{
throw new Exception(ex.Message);
}
is nonsense, it removes information of the original exception like the stack trace. Better remove the catch part (try-finally without a catch is still valid).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900