Consulting

Results 1 to 4 of 4

Thread: Solved: Different versions of References

  1. #1
    VBAX Newbie
    Joined
    Jan 2008
    Posts
    2
    Location

    Solved: Different versions of References

    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

  2. #2
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    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.

    [VBA]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[/VBA]
    [VBA]
    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
    [/VBA]
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Newbie
    Joined
    Jan 2008
    Posts
    2
    Location
    Thanks for the help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •