PDA

View Full Version : Solved: Different versions of References



macly
01-01-2008, 10:02 PM
Hi All,

On occasions I have had an issue, where I write some VBA using a reference. When this VBA gets moved to a different operating environment I get a MISSING Reference, because the new environment has a different version of the reference. Rather than writing different versions for different platforms, I wanted a more dynamic approach, and thought that someone on this forum may have dealt with it before.

Any ideas?

Thanks

Oorang
01-01-2008, 10:17 PM
Yep this is called "Late Binding". Instead of dimming the object to something specific (Ex: Dim fso as Scripting.FileSystemObject) you declare it as a generic Object (Ex: Dim fso as Object). Then when you instantiate the object-variable instead of using the "New" syntax (Ex: Set fso = New Scripting.FileSystemObject) you use the "CreateObject" syntax (Ex: Set fso = VBA.CreateObject("Scripting.FileSystemObject")).
The down side is that the intellisense (the helpful little prefill thingy) won't work when you late bind. So some people like to write the program early bound, then when done, convert to late bound and remove the references. Another downside is that their is a very slight performance hit when you late bind. In the majority of cases you will never notice it at all, but if you notice a significant performance hit that's a place to look.

Option Explicit
Sub EarlyBoundExample()
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
MsgBox "You have " & fso.Drives.Count & " drives mapped to this PC."
Set fso = Nothing
End Sub

Option Explicit
Sub LateBoundExample()
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
MsgBox "You have " & fso.Drives.Count & " drives mapped to this PC."
Set fso = Nothing
End Sub

Bob Phillips
01-02-2008, 02:20 AM
Where possible you should use early binding as it is far more efficient and you get intellisense when you are developing the code. To avoid the reference conflicts, you should develop on the lowest version that you will deploy the app to. Not only does this mean that you avoid reference conflicts, but it also ensures that you do not use features that earlier versions do not support.

macly
01-02-2008, 02:44 PM
Thanks for the help