Unless you can guarantee that all of your users have the same version of Excel installed as you do, I would strongly recommend converting to Late-bound for software that you release "into the wild".
The down-side to using this is that you no longer get some of the nice features of the VB IDE (like the drop-down lists of properties and methods that appear when you type oXLApp
. ), so it may be a good idea to convert to this method once your code is finished.
There are four steps to convert your code:
1)
Replace Excel data types with Object.
If you have:
VB Code:
- Dim oXLApp as Excel.Application
- Dim oXLBook as Excel.Workbook
- Dim oXLSheet as Excel.Worksheet
- ...
You need to change it to:
VB Code:
- Dim oXLApp as Object
- Dim oXLBook as Object
- Dim oXLSheet as Object
- ...
Note that you should also do this with any other variables that you have declared as
Excel.something
2)
Change the initialisation of the application object.
If you have: VB Code:
- Set oXLApp = New Excel.Application
You should replace it with:
VB Code:
- Set oXLApp = CreateObject("Excel.Application")
Note: if Excel isn’t installed, this line of code will cause an error – you should deal with this by using error handling in an appropriate way for your project.
3)
Define the constants that you have used.
Excel constants (e.g.: xlLeft) are defined in the object library; however you will no longer have a link to this, so they wont be defined any more!
There are two main options here; the first is to use the Object Browser to find the values/declarations for each one you have used (very slow, and prone to errors!), and the other (much simpler) is to add a pre-made module to your project which declares them all for you. You can find a link to one which Microsoft produced in the “useful functions and downloads” section (post #13).
4)
Remove "Excel object Library" from the list in "Project" -> "References"
Your code is now late-bound, and should work with all versions of Excel that support automation (as long as you haven’t used special functionality which wasn’t available in earlier versions).