Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

Simple way to Export FlexGrid to Excel

$
0
0
I see many people asking how to export a VB6 MSFlexgrid to an Excel workbook....this short code with a couple of simple For-Loops is an easy way to do it (Make sure you have a REFERENCE to MS Excel in your project).

Code:

Private Sub smnuExportExcel_Click()
    Dim oExcel As excel.Application
    Dim oWb As excel.Workbook
    Dim oSheet As excel.Worksheet
    Dim x As Integer
    Dim y As Integer
    Set oExcel = New excel.Application
    Set oExcel = CreateObject("Excel.Application")
    Set oWb = oExcel.Workbooks.Add
    Set oSheet = oWb.Worksheets("Sheet1")
    With oSheet
    For x = 0 To flexgrid1.Rows - 1 
        For y = 0 To 6
            .Cells(x + 1, y + 1) = flexgrid1.TextMatrix(x, y) 'Note, "x + 1" as Excel refers to rows and columns beginning with 1, whereas VB6's flexgrids start with 0.
        Next y
    Next x

    oWb.SaveAs FileName:=App.Path & "\myExcelFile.xlsx"
    oExcel.Visible = True
    Set oWb = Nothing
    Set oExcel = Nothing
End Sub


Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>