Documente Academic
Documente Profesional
Documente Cultură
until the fifth row ' by using a MsgBox in Visual Basic 6. ' Including the technique how you should access Excel ' step by step properly, from opening the Excel file until ' close and clear the memory that used by Excel object. 'Author : Masino Sinaga 'Date : Sunday, August 2, 2009 'Preparation: 1. Create a new Standard exe project with 1 form ' 2. Add 1 CommandButton control on the Form1 ' 3. Add an item "Microsoft Excel X.X Object Library" ' from menu Project -> References... . X.X depends on ' version Excel or Microsoft Office you are using. ' When I tried this code, it had been installed ' Microsoft Office 2000 and Microsoft Office 2007, ' thus, X.X above is equal with 12.0 which VB6 ' displaying only the last version object library Excel. ' In this case, Microsoft Excel 12.0 Object Library ' 4. Ensure your Dataku.xls file already exists in the same ' directory where your application located, and in ' column B started from the first row until the fifth row ' you already have value in it. ' 5. Copy the following code to the Form1 editor. '-----------------------------------------------------------------Dim Excel As Excel.Application 'Excel Application Dim ExcelWBk As Excel.Workbook 'Excel Workbook Dim ExcelWS As Excel.Worksheet 'Excel Worksheet Private Sub Command1_Click() On Error GoTo Err
'Open Dataku.xls file that located in the same directory 'where your application located.
Set ExcelWBk = Excel.Workbooks.Open(App.Path & "\Dataku.xls")
'Access the first Worksheet (1) 'If you want to switch to the second Worksheet, then 'simply replace (1) with (2), and so forth...
Set ExcelWS = ExcelWBk.Worksheets(1)
'Assign to a variabel
strData = strData & .Cells(i, 2) & vbCrLf Next i End With
'Don' forget neither, always clean-up the memory that 'has just been used by Excel object
ClearExcelMemory
MsgBox "Finish, that's all folks ...!", vbInformation, "Good" Exit Sub Err:
'First of all, get the Excel object, and if error occured 'jumpt to the Err label on the bottom of this Sub, 'then create the Excel object. Typically, error occured here 'if the Excel object has not been created, yet
Set Excel = GetObject(, "Excel.Application") Exit Sub Err:
'Clean-up the memory, check first, whether 'the Excel object exists or not in the memory ...
If Not ExcelWS Is Nothing Then Set ExcelWS = Nothing If Not ExcelWBk Is Nothing Then Set ExcelWBk = Nothing If Not Excel Is Nothing Then Set Excel = Nothing End Sub