Cari Blog Ini

01 April 2013

How To Show or Load Data in File Microsoft Excel Using Microsoft Visual Basic 6 Application




Apply To:  
Microsoft Visual Basic 6
Micorosft Excel 2010

Problem: 

Want to show or load data in file Microsoft Excel Using Microsoft Visual Basic 6 Application





Solution:

1. Create an example of Project, example name : "PrjShowToExcel.vbp"

2. Create an form, example : "frmShowToExcel.frm"



3.
In tab "Project > References...", add for
a. Microsoft Excel 14.0 Object Library
b. Microsoft Office 14.0 Object Library


4. At Form "frmShowToExcel.frm", double-clik for Button "cmdShowToExcel"

5. At code "Private Sub cmdShowToExcel_Click()", add the code below, 
'----------------------------------------------------------------------------------
'----------------------------------------------------------------------------------
Private Sub cmdShowToExcel_Click()

    Rem General Declaration
    Dim objExcel As Excel.Application
    Dim objWorkBook As Excel.Workbook
    Dim objWorkSheet As Excel.Worksheet


    Rem Initialization of variables
    Set objExcel = New Excel.Application
    Set objWorkBook = objExcel.Workbooks.Add
    Set objWorkSheet = objWorkBook.Worksheets("Sheet1")
    Set objWorkSheet = objExcel.ActiveSheet

    objWorkSheet.Range("A1").Value = "Test Test Test"
    objExcel.Visible = True ' The created excel workbook would be visible.
    objWorkBook.Close (True) 'This will fire the SaveAs dialog box

End Sub
'----------------------------------------------------------------------------------
'----------------------------------------------------------------------------------





6. Try to run the code, click "Start" or go to menu "Run > Start (F5)"


7. Example the application, click the button "Show  To Excel"

8. Fill the name, example : "Results.xlsx", then click "Save"

9. Check the file, 




Source:
http://p2p.wrox.com/beginning-vb-6/22635-writing-data-excel-vb6.html