Guys,

I am using the code below to get all rows from a datatable into a multidim array, I am then writing each array element to a new row in an Excel workbook.

I am getting a problem if any of the elements have more than 255 chars, I believe this is the issue anyway. It starts copying fine, but then when I get to what could be a large text field, I get an error.... "Exception from HRESULT: 0x800A03EC"

How can I only copy the first 255 chars from an array element ? I have marked below where I believe I need to make the change, or would it be when populating the array initially ?

Thanks in advace,
Bob


VB Code:
  1. Public Shared Sub Export(ByVal Filename As String, ByVal dt As DataTable, ByVal MRSMaster As String)
  2.  
  3.             Dim RowNbr As Int32 = 0
  4.             Dim AllRecords(dt.Rows.Count) As Object
  5.  
  6.             Try
  7.  
  8.                 'get MatRec into array
  9.                 For Each Rw As DataRow In dt.Rows
  10.                     AllRecords(RowNbr) = Rw.ItemArray()
  11.                     RowNbr += 1
  12.                 Next Rw
  13.  
  14.                 Dim oExcel As New Excel.Application
  15.                 Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook
  16.                 Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet
  17.                 Dim oCells As Excel.Range
  18.                 'oExcel.Visible = False : oExcel.DisplayAlerts = False
  19.  
  20.                 'Open MRS template
  21.                 oBook = oExcel.Workbooks.Open(MRSMaster)
  22.  
  23.                 oSheets = oBook.Worksheets
  24.                 oSheet = CType(oSheets.Item(1), Excel.Worksheet)
  25.                 oExcel.AddIns("Analysis ToolPak").Installed = True
  26.  
  27.                 'Do all the data export here.
  28.                 Dim x As Integer
  29.                 Dim y As Integer
  30.                 Dim range As String
  31.                 For x = 0 To AllRecords.GetUpperBound(0)
  32.  
  33. '************** HERE ***************
  34.  
  35.                     range = "A" & x + 2 & ":Q" & x + 2
  36.                     oSheet.Range(range).Value = AllRecords(x)
  37.                 Next
  38.  
  39.                 Select Case Right(MRSMaster, 13)
  40.                     Case "antMaster.xls"
  41.                         oSheet.Columns(17).delete()
  42.                         oSheet.Columns(13).delete()
  43.                         oSheet.Columns(2).delete()
  44.                         oSheet.Columns(1).delete()
  45.                     Case "MRSMaster.xls"
  46.                         oSheet.Columns(14).delete()
  47.                         oSheet.Columns(5).delete()
  48.                         oSheet.Columns(2).delete()
  49.                         oSheet.Columns(1).delete()
  50.                 End Select
  51.  
  52.  
  53.                 Dim MyDateTime As Date = Today
  54.                 Dim MyStr As String
  55.                 MyStr = Format(MyDateTime, "dd-mm-yy")
  56.                 oSheet.Name = "PWBExport " & MyStr
  57.  
  58.  
  59.                 oBook.SaveAs(Filename)
  60.                 oBook.Close()
  61.                 oExcel.DisplayAlerts = True
  62.  
  63.                 'Quit Excel and thoroughly deallocate everything
  64.                 oExcel.Quit()
  65.                 oExcel = Nothing : oBooks = Nothing : oBook = Nothing
  66.                 oSheets = Nothing : oSheet = Nothing
  67.                 AllRecords = Nothing
  68.  
  69.             Catch ex As Exception
  70.                 EH.Log(ex)
  71.             End Try
  72.         End Sub