1

I am getting this error Exception from HRESULT: 0x800A03EC in excel.range.
Here is my following code

    Private Sub excelInsertData(rowIndex As Integer, ColIndex As Integer, EDT As DataTable)
    Try

        Dim arr As Object(,) = New Object(EDT.Rows.Count - 1, EDT.Columns.Count - 1) {}

        For r As Integer = 0 To EDT.Rows.Count - 1
            Dim dr As DataRow = EDT.Rows(r)

            For c As Integer = 0 To EDT.Columns.Count - 1
                arr(r, c) = dr(c)
            Next
        Next

        Dim c1 As Excel.Range = CType(xlWorkSheet.Cells(rowIndex, ColIndex), Excel.Range)
        Dim c2 As Excel.Range = CType(xlWorkSheet.Cells(rowIndex + EDT.Rows.Count - 1, EDT.Columns.Count + ColIndex - 1), Excel.Range)
        Dim range As Excel.Range = xlWorkSheet.Range(c1, c2)
        range.Value = arr '---------ERROR THROWN HERE

    Catch ex As Exception
        MsgBox(ex.toString)
    Finally
        EDT.Rows.Clear() : EDT.Columns.Clear()
    End Try
End Sub

I am trying to do is transferring data from a Datatable (EDT in this case) to an Excel Template.

Following are the values range, arr and EDT.

excel.range
range.Value - Length is 5

arr value
same length as range

EDT
Value in my DataTable(EDT)

Also, I can attach a template of my 3b.xlsm file if required.

Please ask if I am missing any information which needed to be provided.

bonny
  • 688
  • 1
  • 14
  • 33
  • please check this out: `New Object (EDT.Rows.Count - 1, EDT.Columns.Count - 1)` and `For r As Integer = 0 To EDT.Rows.Count - 1` – Dang D. Khanh Dec 10 '20 at 09:54
  • And what to check in that ? Thats just array size declaration. @Dang D. Khanh – bonny Dec 11 '20 at 01:34
  • I think it could be: `Dim arr (EDT.Rows.Count - 1, EDT.Columns.Count - 1) As Object` instead. – Dang D. Khanh Dec 11 '20 at 05:25
  • If I truely say, I copied this from Stack in search of 'How to insert data to excel using range' so I may try this as you suggested but I don't think this is problem. @DangD.Khanh – bonny Dec 11 '20 at 10:46
  • There must be something else going on. In my tests using your posted code, I got NO errors as you describe, and the code ran successfully with a data table of 10 columns and 100 rows in a matter of seconds. You say the original file is a template with macros, and in my tests, this made no difference. Have you tested this code with possibly a different file to at least to eliminate THAT file as the problem? – JohnG Feb 22 '21 at 13:20
  • I overcome this issue with some method I guess with `excel.range` only. It's definitely not solved and still show this error but I did found the other way that I don't remember now. The 3b.xlsm gave me an idea of where it is but I have to recreate the error and tell you what was happening. @JohnG – bonny Feb 22 '21 at 14:08
  • I have tried may things to get the code to throw an error/crash. Even if the two ranges are different sizes no error was thrown. Null cells etc. it never crashed. If you have a work around then I will assume that is acceptable to you. – JohnG Feb 22 '21 at 14:17

1 Answers1

0

The first line just gets the DataTable from a Function. You could also just add a parameter to the Sub and pass one in. This code creates a new Excel file. The first loop adds the column names and the second loop adds the data.

Private Sub FillExcelFromDataTable()
    Dim dt = GetWorksheetData()
    Dim oExcel As New Excel.Application
    Dim oBook = oExcel.Workbooks.Add
    Dim oSheet = DirectCast(oBook.Worksheets.Add, Excel.Worksheet)
    Dim ColumnIndex = 1 'in Excel worksheet
    For Each col As DataColumn In dt.Columns 'This loop adds the header row
        oSheet.Cells(1, ColumnIndex) = col.ColumnName
        ColumnIndex += 1
    Next
    ColumnIndex = 1 'The columns and rows in the spreadsheet
    Dim RowIndex = 2 'The columns and rows in the spreadsheet
    For rowI = 0 To dt.Rows.Count - 1
        For Each col As DataColumn In dt.Columns
            oSheet.Cells(RowIndex, ColumnIndex) = dt(rowI)(col)
            ColumnIndex += 1
        Next
        ColumnIndex = 1 'Reset back to the first column
        RowIndex += 1
    Next
    oBook.Save()
    oBook.SaveAs(Filename:="ExcelDat.xlsx") 'Saved to "C:\Users\xxx\Documents\ExcelDat.xlsx"
    oBook.Close()
    oExcel.Quit()
End Sub
Mary
  • 14,926
  • 3
  • 18
  • 27
  • This what I tried at first. What you did is adding value to a particular cell at a time. I did that but Microsoft interop services is damn slow. I have an SSD and it took about 5 minutes to fill 20x10 (rowsxcolumns) of data. So I researched internet and come to know that add data using range instead of using like this. Irony is that my code runs well without errors in other method I declared. Main problem is I can't understand what the error trying to say. If I won't catch it then it passes and file is generated perfectly but thats no good. Also I have a macro file .xlsm. – bonny Dec 11 '20 at 01:28
  • Agreed. Interop is not the best. Take a look at EPPlus. https://www.epplussoftware.com/ You can try it out for free. I think it has a method that would be useful. `ws.Cells["A1"].LoadFromDataTable(dataTable, true);` – Mary Dec 11 '20 at 02:04
  • I tried EPPlus, OpenXML, ClosedXML(using ClosedXML from a long time but it recent updates started making an issue of corrupting my template file. I also opened an issue on ClosedXML git) everything but my template files are a headache for me. I have to shift to interop because it won't corrupt files. The best method I found is Range to transfer. It transfer more speedily. All data transfers in approx 3-4 minutes. – bonny Dec 11 '20 at 06:21
  • but will this `ws.Cells["A1"].LoadFromDataTable(dataTable, true)` thing adjust the cols and rows of datatable? Like if I wanna start from B11 cell and a row goes till F11 so will it write in next line? Asking because I am afraid to touch current code. – bonny Dec 11 '20 at 12:13
  • @bonny See https://riptutorial.com/epplus/example/26422/fill-with-a-datatable and on SO https://stackoverflow.com/questions/13669733/export-datatable-to-excel-with-epplus – Mary Dec 11 '20 at 21:58
  • everything is right but as I already said I used EPPlus before. It even works better when filling data in a blank excel but the templates I am using are heavily loaded with macros and it corrupts the file. I can give you the excel file if you want to try it. – bonny Dec 16 '20 at 13:36