Export MSHFlexgrid data to Excel

Craigg
03-24-2003, 02:54 PM
Can someone tell me a way to export the data that I have displayed in a MSHFlexgrid to an Excel Spreadsheet?

Thanks

Mikecrosoft
03-24-2003, 03:07 PM
You need loop through each MSHFlexGrid Cell and send the text to a specified Excel Cell using the Excel Object Library:

Here is one example made by me about open and send data to excel:

http://www.visualbasicforum.com/attachment.php?attachmentid=6130

PWNettle
03-24-2003, 03:29 PM
I'll go ahead and add my reply too even though Mikecrosoft has pretty much answered you (I was typing up my reply as he replied, I imagine).

This topic has been approached a few times at this forum, so you can always search for more examples.

Basically you need to do some simple Excel automation which requires a reference to Excel...and as Mikecrosoft indicates...you need to loop on your flexgrid to 'copy' the contents to Excel.

Here's some simple code that illustrates how one might do it:

' Requires a reference to the Microsoft Excel x.x Object Library.
Dim appExcel As Excel.Application
Dim lngRows As Long
Dim lngCols As Long

Set appExcel = New Excel.Application

appExcel.Workbooks.Add

' Alter For...Next loops as needed if you use fixed rows and/or columns on your grid.
For lngRows = 0 To MSFlexGrid1.Rows - 1
For lngCols = 0 To MSFlexGrid1.Cols - 1
appExcel.Cells(lngRows + 1, lngCols + 1) = MSFlexGrid1.TextMatrix(lngRows, lngCols)
Next lngCols
Next lngRows

' Change the save path as needed....
appExcel.ActiveWorkbook.SaveAs App.Path & "\flexgrid.xls"

appExcel.Quit
Set appExcel = Nothing

Beep

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum