0

I have a workbook which checks if there is a new version of some module when someone opens it and in that case it removes the old module and import the new one.

No problem until i add some new references and i try aswell to import them programatically.

When i try to do this the code fails to compile cause i'm using libraries which are not yet imported. Is there any way to solve this using early binding?

Thank you in advanced.

Community
  • 1
  • 1
RoberFJR
  • 1
  • 1
  • 1
    Possible duplicate of [How to add a reference programmatically](http://stackoverflow.com/questions/9879825/how-to-add-a-reference-programmatically) – Ron Rosenfeld Sep 06 '16 at 11:07
  • I'm trying to solve a different problem. No problem adding references programmatically, the problem is doing it with code **already using the references** i'm trying to add. – RoberFJR Sep 06 '16 at 14:28
  • It doesn't really matter whether the code you want to use already contains the references. You need to first run code that will add those references that are not already referenced. In any event, there is no way of doing this, that I know of, without changing the Trust Center settings to allow access to the VBA Project Object model, which needs to be done at that computer. **Late Binding** is probably going to be your best solution. – Ron Rosenfeld Sep 06 '16 at 14:31
  • If i don't use the reference i won't get any error running it. The problem is when i try to use a missing reference. It is explained in this answer in the thread you mentioned. http://stackoverflow.com/a/9880260/6094449 Thank you. – RoberFJR Sep 06 '16 at 14:47
  • Maybe I am not understanding something. But the sequence, as I see it, is **1:** test to see if the references your routine requires have been added. **2:** add the missing references, **3** Then run your macro. – Ron Rosenfeld Sep 06 '16 at 16:15
  • I'm trying to do it the other way around. **1:** Run the macro, **2:** Add the references programatically, **3:** Use referenced routines. The macro runs at workbook.open, checks if there is a new version of the code and in that case updates the code programatically. Works well until i need to use some new library(at this point i have to import the library manually and just what i'm trying to avoid) I guess the simplest solution is making 2 updates, first just import the new references, and in the next update the code using the references. – RoberFJR Sep 06 '16 at 22:46

1 Answers1

1

The best way to overcome this type of issue is to use late bindings, this means that all of your object variables are simply declared as Objects which allows for the user's computer to allocate the correct binding when the macro is run instead of before. You can find more information on this here and here

Jordan
  • 4,424
  • 2
  • 18
  • 32