Using Excel from VB 6 (referencing Excel Dll)

Posted on 2004-04-28
Last Modified: 2012-06-21

If there are different versions being used for Excel in the company.
How do I make sure that my program doesn't break if I reference Excel lib 10 from my program and users have older or newer versions of excel installed on their machines.

Question by:jupiterz
LVL 24

Accepted Solution

R_Rajesh earned 250 total points
ID: 10941658

use late binding. check out the accepted comment in this link:


Author Comment

ID: 10941851
   Looking at the accepted answer, I modified my code which is listed below.
    My question is if i just change the declaration of the xlApp variable from
   Dim xlApp As  Excel.Application to
   dim xlApp as Object  Does it mean I am late binding? I tested my code it works fine.
   Looking at the code if you can tell me i have done late binding or if it requires any other changes

    Dim sFileName As String
    Dim sPath As String
    Dim xlApp As Object  'Excel.Application
    Dim xlBook As Object 'Excel.Workbook
    Dim xlSheet As Object 'Excel.Worksheet
    Dim i As Integer
    Dim j As Integer
    Dim fld As ADODB.Field

    sFileName = "Report1.xls"
    frmTestQueries.cdb.Filter = "Excel Docs (*.xls)|*.xls"
    frmTestQueries.cdb.FileName = sFileName
    sPath = frmTestQueries.cdb.FileName
    'open the excel spreadsheet for write
    Set xlApp = CreateObject("Excel.Application")
     Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets.Item(1)
    'Get other rows
    For i = 1 To rs.RecordCount
        For j = 0 To rs.Fields.Count - 1
            If i = 1 Then
                xlSheet.Cells(1, j + 1).Value = rs(j).Name
            End If
            xlSheet.Cells(i, j + 1).Value = rs(j).Value
        Next j
    Next i
    'format cell column widths
    xlBook.SaveAs (sPath)
    Set xlApp = Nothing
    Set xlBook = Nothing
    Set xlApp = Nothing
LVL 24

Expert Comment

ID: 10942009
the code looks fine. ofcourse, you can always post back if you run into trouble in future :)
you might want to go through this:

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 26

Expert Comment

ID: 10942171
Also make sure you take out all excel reference from References

Author Comment

ID: 10942177
Thanks Rajesh. Got what I asked for...
LVL 24

Expert Comment

ID: 10942237
thanks for the grade, jupiterz

Expert Comment

ID: 11291292
This code was really helpful.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Basic Excel Formatting error 4 140
Run code from text file in vb 1 84
Visual Studio search word table and return Cell index 8 74
transition to visual .net from vb6 5 57
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question