Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to create an excel work book at run time using vb.net ,then adding multiple
sheets to it (sheet count depends on a variable Value ) and rename the sheets then fill each sheet with data from SQL server table,when running my code an exception thrown, i google it but not solved yet ,the exception "Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) in excel using vb.net"
it occurs at the sentence :xlWorkSheet = xlWorkBook.Sheets(xlWorkBook.Sheets(i).Name())
,any useful thoughts!!

[EDIT]
VB
Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
If xlApp Is Nothing Then
MessageBox.Show("Excel is not properly installed!!")
Exit Sub
End If

          Dim xlWorkBook As New Excel.Workbook
          xlWorkBook = xlApp.Workbooks.Add()
          Dim xlWorkSheet As New Excel.Worksheet

          Dim dtVacations As DataTable
         'here i fill the datatable with data

          If dtVacations IsNot Nothing AndAlso dtVacations.Rows.Count > 0 Then
              xlWorkSheet = CType(xlApp.Worksheets.Add(, , dtVacations.Rows.Count - 1), Excel.Worksheet)

' Dim Var As Int32 = xlWorkBook.Worksheets.Count
              Dim i As Int32 = 1
              For Each row In dtVacations.Rows
                  xlApp.Visible = True
                  xlWorkSheet = xlWorkBook.Worksheets(i)
                  xlWorkSheet.Activate()
                  '  xlApp.Worksheets("secondsheet").Move(After:=xlWorkBook.Worksheets(xlWorkBook.Worksheets.Count))
                  'xlWorkBook.Sheets(i).Select()
                  xlWorkBook.Sheets(i).Name() = NS(row("VacationNameA"))

                  xlWorkSheet.Cells(1, 1).Value = "EmpNo"
                  xlWorkSheet.Cells(1, 2).Value = "EmpName"
                  xlWorkSheet.Cells(1, 3).Value = "HireDate"
                  xlWorkSheet.Cells(1, 4).Value = "VacNo"
                  xlWorkSheet.Cells(1, 4).Value = "VacName"
                  xlWorkSheet.Cells(1, 5).Value = "VacDaysNo"
                  xlWorkSheet.Cells(1, 6).Value = "BalanceDate"
                  xlWorkSheet.Cells(1, 7).Value = "DueDate"
                  xlWorkSheet.Cells(1, 8).Value = "Bal"
                  xlWorkSheet.Cells(1, 9).Value = "ToDate"

                  'here i get the data from sql server and fill it into datatable
                  Dim dtEmpVac As DataTable = CreateDataTable(MySql)
                  If dtEmpVac IsNot Nothing AndAlso dtEmpVac.Rows.Count > 0 Then
                      For i = 0 To dtEmpVac.Rows.Count - 1
                          'Column
                          For j = 0 To dtEmpVac.Columns.Count - 1
                              ' this i change to header line cells >>>
                              xlWorkSheet.Cells(i + 3, j + 1) = _
                              dtEmpVac.Rows(i).Item(j)
                          Next
                      Next
                  End If

                  i += 1
              Next

          ElseIf dtVacations IsNot Nothing And dtVacations.Rows.Count = 0 Then
                              releaseObject(xlWorkBook)
              releaseObject(xlApp)
              Exit Sub
          End If

          ''Save the workbook

          xlWorkBook.SaveAs(My.Computer.FileSystem.SpecialDirectories.Desktop & "\Vacations.xls", Excel.XlFileFormat.xlWorkbookNormal, _
           Excel.XlSaveAsAccessMode.xlExclusive)
          xlWorkBook.Close(True)
          xlApp.Quit()

          releaseObject(xlWorkBook)
          releaseObject(xlApp)


The problem occurs after the first loop when go to next sheet!!!
[/EDIT]
Posted
Updated 5-Oct-15 8:57am
v2
Comments
Richard MacCutchan 3-Oct-15 7:27am    
Yes, the value of i is not valid.
Patrice T 3-Oct-15 7:38am    
Show the VB program

1 solution

Quote:
Microsoft Excel creates a new workbook with a number of blank sheets (the number of sheets is set by the SheetsInNewWorkbook property[^]

source: Workbooks.Add method[^]


For further information on how to create new workbook and worksheets, please see:
How to: Programmatically Create New Workbooks[^]
How to: Programmatically Add New Worksheets to Workbooks[^]
 
Share this answer
 
Comments
Member 11279959 5-Oct-15 4:40am    
thank you for your help, but it still unsolved.
-- code has been moved into question --
Maciej Los 5-Oct-15 16:02pm    
You did not read my answer carefully. I mentioned there that you have to change SheetsInWorkbook property to be able to create new workbook with the number of worksheets inside.

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