Google

Monday, June 23, 2008

i-TimeTabler Notes: Creating Excel file, and loading into VB6 Form

I am now able to programmatically create an Excel Application,
populate it with data with modified cells, and then load
the Excel worksheet into the VB6 Form as an OLE object.



Source Code:


Text version:

'---- Early Binding--------
'Dim xlApp As Excel.Application
'Dim xlBook As Excel.Workbook
'Dim xlSheet As Excel.Worksheet


'----- Late Binding --------
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object

Private Sub Form_Load()
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlApp.DisplayAlerts = False
End Sub

Private Sub cmdCreateExcel_Click()
'xlApp.Application.Visible = True
xlSheet.Cells(1, 3).Value = "CSC200" & Chr(10) & "LR511"
With xlSheet
.Range(xlSheet.Cells(1, 3), xlSheet.Cells(1, 4)).MergeCells = True
'.Range("C1:D1").MergeCells = True
.Range("C1:D1").Font.Bold = True
.Range("C1:D1").Font.ColorIndex = 51
.Range("C1:D1").ColumnWidth = 6
.Range("C1:D1").RowHeight = 35
.Range("C1:D1").HorizontalAlignment = xlDistributed
.Range("C1:D1").VerticalAlignment = xlDistributed
End With
xlBook.SaveAs App.Path & "\MyFile.xls"
OLE1.CreateLink SourceDoc:=App.Path & "\MyFile.xls"
OLE1.Refresh
Call CleanUp
End Sub

Private Sub CleanUp()
'xlApp.Application.Visible = False
Set xlSheet = Nothing
'xlBook.Close SaveChanges:=False
Set xlBook = Nothing
xlApp.Application.Quit
Set xlApp = Nothing
End Sub