Connecting Tech Pros Worldwide Forums | Help | Site Map

How to extract the records from datagrid to excel

Newbie
 
Join Date: May 2007
Posts: 2
#1: May 7 '07
I have developed an application for extracting records from datagrid to excel. Unfortunately, when i run this application, no records were extracted. If anybody knows what is lacking in my script then please inform me. Below is my script. Thanks a lot!!!

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmDExtract_Click()
  2.  
  3.     On Error GoTo ErrorLine
  4.     Screen.MousePointer = vbHourglass
  5.     Dim xl As Object
  6.     Dim wb As Object
  7.     Dim ws As Object
  8.     Dim i  As Long
  9.     Dim X  As Long
  10.  
  11.     Set xl = CreateObject("Excel.Application")
  12.     Set wb = xl.Workbooks.Add()
  13.     Set ws = xl.ActiveWindow.ActiveSheet
  14.     ws.Columns().ColumnWidth = 10
  15.  
  16.     For X = 0 To DataGrid1.Row - 1
  17.         DataGrid1.Row = X
  18.         DataGrid1.Col = 1
  19.             For i = 1 To DataGrid1.Col
  20.                 DataGrid1.Col = i
  21.                 If DataGrid1.Text <> "" Then
  22.                     ws.Cells(X, i) = DataGrid1.Text
  23.                     ws.Cells(X, i).HorizontalAlignment = -4131
  24.  
  25.                 End If
  26.             Next
  27.     Next
  28.     xl.Visible = True
  29. ErrorLine:
  30.     Screen.MousePointer = vbDefault
  31.  
  32. End Sub

Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,268
#2: May 7 '07

re: How to extract the records from datagrid to excel


Quote:

Originally Posted by edgara

I have developed an application for extracting records from datagrid to excel. Unfortunately, when i run this application, no records were extracted. If anybody knows what is lacking in my script then please inform me. Below is my script. Thanks a lot!!!

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub cmDExtract_Click()
  3.  
  4.     On Error GoTo ErrorLine
  5.     Screen.MousePointer = vbHourglass
  6.     Dim xl As Object
  7.     Dim wb As Object
  8.     Dim ws As Object
  9.     Dim i  As Long
  10.     Dim X  As Long
  11.  
  12.     Set xl = CreateObject("Excel.Application")
  13.     Set wb = xl.Workbooks.Add()
  14.     Set ws = xl.ActiveWindow.ActiveSheet
  15.     ws.Columns().ColumnWidth = 10
  16.  
  17.     For X = 0 To DataGrid1.Row - 1
  18.         DataGrid1.Row = X
  19.         DataGrid1.Col = 1
  20.             For i = 1 To DataGrid1.Col
  21.                 DataGrid1.Col = i
  22.                 If DataGrid1.Text <> "" Then
  23.                     ws.Cells(X, i) = DataGrid1.Text
  24.                     ws.Cells(X, i).HorizontalAlignment = -4131
  25.  
  26.                 End If
  27.             Next
  28.     Next
  29.     xl.Visible = True
  30. ErrorLine:
  31.     Screen.MousePointer = vbDefault
  32.  
  33.  
  34.  
  35. End Sub

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
Hey there, edgara!

Good of you to add your code in. It looks like you have not yet attempted to create a .xls file. If you have previously searched here and could not find anything, set your eyes on this:

http://www.developerfusion.co.uk/show/6072/

This should get you started. If not, please stay tuned. I cannot run anything from here just yet. Please stay tuned nonetheless, helpful members will likely see your information.

In a bit!

Dököll
Reply