Solved

Using Excel from VB 6 (referencing Excel Dll)

Posted on 2004-04-28
7
939 Views
Last Modified: 2012-06-21
Hi,

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.

Thanks
0
Comment
Question by:jupiterz
7 Comments
 
LVL 24

Accepted Solution

by:
R_Rajesh earned 250 total points
ID: 10941658
Jupiterz,

use late binding. check out the accepted comment in this link:
http://www.experts-exchange.com/Applications/MS_Office/Excel/Q_20947782.html

Rajesh
0
 

Author Comment

by:jupiterz
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
    frmTestQueries.cdb.ShowSave
   
    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
    rs.MoveFirst
    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
    rs.MoveNext
    Next i
   
    'format cell column widths
    xlSheet.Columns.AutoFit
   
    xlBook.SaveAs (sPath)
    xlApp.Quit
   
    Set xlApp = Nothing
    Set xlBook = Nothing
    Set xlApp = Nothing
0
 
LVL 24

Expert Comment

by:R_Rajesh
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:
http://word.mvps.org/FAQs/InterDev/EarlyvsLateBinding.htm

Rajesh
0
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.

 
LVL 26

Expert Comment

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

Author Comment

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

Expert Comment

by:R_Rajesh
ID: 10942237
thanks for the grade, jupiterz
:)
R
0
 

Expert Comment

by:reena_pathak
ID: 11291292
Thanks,
This code was really helpful.
-Reena
0

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

792 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