0% found this document useful (0 votes)
356 views

Writing To Ms Excel Using Vb6

This document provides code examples to automate Microsoft Excel from a VB6 application. It shows how to open and interact with an Excel workbook programmatically: adding values to cells from text boxes, retrieving cell values, saving the workbook. The code declares Excel object variables, loads values on form load, and quits Excel on form unload. Buttons trigger clicks to perform tasks like opening a file, adding/retrieving values, and saving.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
356 views

Writing To Ms Excel Using Vb6

This document provides code examples to automate Microsoft Excel from a VB6 application. It shows how to open and interact with an Excel workbook programmatically: adding values to cells from text boxes, retrieving cell values, saving the workbook. The code declares Excel object variables, loads values on form load, and quits Excel on form unload. Buttons trigger clicks to perform tasks like opening a file, adding/retrieving values, and saving.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Writing to Ms Excel worksheet via VB6

See example to automate excelAdd 5 buttons and a textbox to a VB6 form. You also need an excel file, excel.xls, save to the save location as the vb6 project.

Option Explicit 'VB6 MENU - PROJECT , REFERENCES, set a refernce to: 'Microsoft Excel 10.0 Object Library Const cMyExcel = "Excel.xls" Dim cPath As String Private mvarExcel As Excel.Application Private Workbook As Object Private Sub Command1_Click() 'SHOW EXCEL AND OPEN A FILE 'workbook.Activate mvarExcel.Visible = True 'SHOW EXCEL mvarExcel.Workbooks.Open cPath & cMyExcel 'OPEN A FILE End Sub Private Sub Command2_Click() 'NEW EXCEL WORKBOOK Dim row As Integer Dim wb As Object Dim ws As Worksheet mvarExcel.Visible = True mvarExcel.Workbooks.Add Set wb = mvarExcel.Application.ActiveWorkbook Set ws = wb.ActiveSheet 'PUT VALUE IN TEXTBOX INTO EXCEL CELLS For row = 1 To 5 ws.Rows.Cells(row, 1) = Text1.Text Next End Sub

Private Sub Command3_Click() 'GET VALUES FROM CELLS Dim row As Integer Dim wb As Object Dim ws As Worksheet Set wb = mvarExcel.Application.ActiveWorkbook Set ws = wb.ActiveSheet 'DISPLAY VALUES IN EXCEL CELLS For row = 1 To 5 MsgBox ws.Rows.Cells(row, 1) Next End Sub Private Sub Command4_Click() 'PUT VALUES INTO CELLS Dim row As Integer Dim wb As Object Dim ws As Worksheet Set wb = mvarExcel.Application.ActiveWorkbook Set ws = wb.ActiveSheet 'PUT TEXTBOX VALUES INTO EXCEL CELLS For row = 1 To 5 ws.Rows.Cells(row, 1) = Me.Text1 + " " & row Next End Sub Private Sub Command5_Click() 'SAVE WORKBOOK mvarExcel.Application.ActiveWorkbook.Save 'mvarExcel.Application.ActiveWorkbook.SaveAs cPath & cMyExcel End Sub

Private Sub Form_Load() 'INIT EXCEL OBJECT Set mvarExcel = New Excel.Application cPath = App.Path + "\" 'MyPath End Sub Private Sub Form_Unload(Cancel As Integer) 'QUIT EXCEL mvarExcel.Quit

Set mvarExcel = Nothing End Sub

You might also like