1

I am trying to add external libraries and use them in VBA for Excel. I have found two questions about it, but I can't get them to work in a real application.

Both of them work fine on their own, but when I try to use them in a program I get a compiler error. It seems that I cannot compile the program before I add the library (makes sense, I use unknown objects) and I cannot add the references before I compile the program.

How can I solve this?

Question 1:
Connect references (Tools>References) with VBA code (macros)

Question 2:
How to add a reference programmatically

Orbit
  • 212
  • 1
  • 7

1 Answers1

0

It seems the references can be added using the Workbook_Open sub in Excel. This sub automatically runs when the workbook is opened. It works fine if it does not call any routines that use objects from the libraries that need to be added.

One of my concerns was that the macro would not run if the user has to enable macros after the book is opened. Most of the information I can find indicate that it should still run. I have not been able to test it yet though, because it seems very difficult to "untrust" a workbook to has been set to "trusted" before.

Orbit
  • 212
  • 1
  • 7
  • Note that the whole sub cannot mention any sub that uses objects from the library to be added. Even if it is behind an if statement, and the call will not get executed, it will still give an error because the sub with unknown objects gets compiled. – Orbit Jul 18 '20 at 12:28