I would like to export my data in DataGrid to excel. It could be sent to excel but there are errors on rows.

I would also like to know how to export the column headers to excel along.

VB Code:
  1. Private Sub cmdExport_Click()
  2.     Dim objExcel As Excel.Application
  3.     Dim objWorkbook As Excel.Workbook
  4.     Dim i As Long
  5.     Dim n As Long
  6. On Error Resume Next
  7.     Set objExcel = GetObject(, "Excel.Application")
  8. If Err.Number Then
  9.    Err.Clear
  10.    Set objExcel = CreateObject("Excel.Application")
  11.    If Err.Number Then
  12.       MsgBox "Can't open Excel."
  13.    End If
  14. End If
  15. objExcel.Visible = True
  16. Set objWorkbook = objExcel.Workbooks.Add
  17. AppActivate "DataGrid To Excel Demo"
  18.  
  19.     For i = 0 To DataGrid1.Row.Count -1 <======= Error here
  20.         DataGrid1.Row = i
  21.     For n = 0 To DataGrid1.Columns.Count - 1
  22.         DataGrid1.Col = n
  23.         objWorkbook.ActiveSheet.Cells(i + 1, n + 1).Value = DataGrid1.Text
  24.     Next
  25.     Next
  26. End Sub