Dim oExcelApp As Excel.Application
Dim oWs As Excel.Worksheet
Dim oWb As Excel.Workbook
Dim ExcelFileName As String
Dim X As Long
ExcelFileName = "text.xls"
If Trim(ExcelFileName) <> "" Then
If Dir(ExcelFileName) <> "" Then Kill (ExcelFileName)
Set oExcelApp = CreateObject("EXCEL.APPLICATION")
oExcelApp.Visible = False
Set oWb = oExcelApp.Workbooks.Add
Set oWs = oExcelApp.Worksheets(1)
oExcelApp.Columns("A").ColumnWidth = 15
oExcelApp.Columns("B").ColumnWidth = 15
oExcelApp.Columns("C").ColumnWidth = 25
oExcelApp.Columns("D").ColumnWidth = 40
oExcelApp.Columns("E").ColumnWidth = 15
oWs.Cells(1, 1).Value = "Field1"
oWs.Range("A1:A1").Select
oWs.Range("A1:A1").Activate
oWs.Range("A1:A1").Font.Bold = True
oWs.Cells(1, 2).Value = "Field2"
oWs.Range("B1:B1").Select
oWs.Range("B1:B1").Activate
oWs.Range("B1:B1").Font.Bold = True
oWs.Cells(1, 3).Value = "Field3"
oWs.Range("C1:C1").Select
oWs.Range("C1:C1").Activate
oWs.Range("C1:C1").Font.Bold = True
oWs.Cells(1, 4).Value = "Field4"
oWs.Range("D1:D1").Select
oWs.Range("D1:D1").Activate
oWs.Range("D1:D1").Font.Bold = True
oWs.Cells(1, 5).Value = "Field5"
oWs.Range("E1:E1").Select
oWs.Range("E1:E1").Activate
oWs.Range("E1:E1").Font.Bold = True
For X = 1 To (MSFlexGrid1.Rows - 1)
oWs.Cells(X + 1, 1).Value = MSFlexGrid1.TextMatrix(X, 0)
oWs.Cells(X + 1, 2).Value = MSFlexGrid1.TextMatrix(X, 1)
oWs.Cells(X + 1, 3).Value = MSFlexGrid1.TextMatrix(X, 2)
oWs.Cells(X + 1, 4).Value = MSFlexGrid1.TextMatrix(X, 3)
oWs.Cells(X + 1, 5).Value = MSFlexGrid1.TextMatrix(X, 4)
Next X
oWs.Range("E2:E" & (MSFlexGrid1.Rows - 1)).Select
oWs.Range("E2:E" & (MSFlexGrid1.Rows - 1)).Activate
oWs.Range("E2:E" & (MSFlexGrid1.Rows - 1)).NumberFormat = "$#,##0.00"
oWb.SaveAs ExcelFileName
oExcelApp.Quit
Set oWs = Nothing
Set oWb = Nothing
Set oExcelApp = Nothing
End If