I always do this for my "print to file" checkbox in the print dialog
**Needed Reference** : Microsoft Excel x.y Object Library
Code:
Public Sub ExportFlexGrid(ByRef objGrid As MSFlexGrid)
Dim objXL As Excel.Application
Dim objWB As Excel.Workbook
Dim objWS As Excel.Worksheet
Dim r As Long
Dim c As Long
Dim intRed As Integer
Dim intGreen As Integer
Dim intBlue As Integer
Set objXL = New Excel.Application
Set objWB = objXL.Workbooks.Add
Set objWS = objWB.Worksheets(1)
With objWS
For r = 0 To objGrid.Rows - 1
For c = 0 To objGrid.Cols - 1
.Cells(r + 1, c + 1) = objGrid.TextMatrix(r, c)
Next
Next
' You can do some nice things, like adding a counter
' .Cells(objGrid.Rows, 7) = "=count(G2:G" & objGrid.Rows - 1 & ")"
.Cells.Columns.AutoFit
End With
objXL.Visible = True
Set objWS = Nothing
Set objWB = Nothing
Set objXL = Nothing
End Sub