I try to open excel file in visual basic 6. Somebody gave me this code to open excel file. But when i create a my new form and type this code. An error occured for this code

Private Sub Form_Load()
Dim x_app As Excel.Application
Dim x_wbk As Excel.Workbook
Dim x_wsh As Excel.Worksheet

Set x_app = New Excel.Application
Set x_wbk = x_app.Workbooks.Add
Set x_wsh = x_wbk.Worksheets.Add

x_wsh.Cells(1, 1) = 50
x_app.Visible = True

End Sub

When I declare

Dim x_app as Excel.Application in my new form it is not accepted It change to Dim x_app as Excelfrm.

How to add Excel in my object browser. Maybe it is the solution to open my excel file in visual basic 6.

Anybody can help me and I appreaciate it alot. Step by step instruction to clearly open excel file in vb6 is served me best.

Thank you in advance.

Recommended Answers

All 2 Replies


Use this code:

Private Sub Command1_Click()

Dim xl As Object
Dim xlwbook As Object
Dim xlsheet As Object

    Set xl = CreateObject("Excel.Application")
    Set xlwbook = xl.Workbooks.Add
    Set xlsheet = xlwbook.Worksheets.Add
    'do your staff example:
    xlsheet.range("A1") = 100
    'finish you staff
    Set xl = Nothing
    Set xlwbook = Nothing

End Sub


If you want to save you work just before xl.quit add: xlwbook.saveas "c:\x.xls"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.