Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 948
  • Last Modified:

Using Excel from VB 6 (referencing Excel Dll)

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
jupiterz
Asked:
jupiterz
1 Solution
 
R_RajeshCommented:
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
 
jupiterzAuthor Commented:
   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
 
R_RajeshCommented:
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
Industry Leaders: 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!

 
EDDYKTCommented:
Also make sure you take out all excel reference from References
0
 
jupiterzAuthor Commented:
Thanks Rajesh. Got what I asked for...
0
 
R_RajeshCommented:
thanks for the grade, jupiterz
:)
R
0
 
reena_pathakCommented:
Thanks,
This code was really helpful.
-Reena
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now