Create An Excel File From Your Program

Create a new Excel file from your program, fill the Excel file cells
with values, and save it.
The user will not see the Excel environment at all!


Add 1 Command Button to your form.
Add 1 reference to Microsoft Excel X.0 Object Library (From
VB Menu choose Project -> References..., mark the Microsoft Excel X.0 Object Library
check box and press OK).

Form Code

Private Sub Command1_Click()

    Dim xlApp As Excel.Application
    Dim xlWB As Excel.Workbook
    Dim xlWS As Excel.Worksheet

    Set xlApp = New Excel.Application
    Set xlWB = xlApp.Workbooks.Add
    Set xlWS = xlWB.Worksheets.Add

' This following lines will fill the cell (2,2) with the text "hello",
' and will fill the cell (1,3) with the text "World"

    xlWS.Cells(2, 2).Value = "hello"
    xlWS.Cells(1, 3).Value = "World"

' The following line saves the spreadsheet to "c:\mysheet.xls" file.
    xlWS.SaveAS "c:\mysheet.xls"

' Free memory
    Set xlWS = Nothing
    Set xlWB = Nothing
    Set xlApp = Nothing

End Sub

Go Back