VBA Declaring a library vs dynamically loading it
I'm creating an application in VBA that uses a reference from vbscript.dll
I'm trying to figure out the best way to load the library, and am having difficulty distinguishing between using a declare function (http://msdn.microsoft.com/en-us/library/aa716201(v=vs.60).aspx) or just dynamically adding it to the list of references (How to add a reference programmatically vba-Excel, part 2 of the top answer)
If anyone can clarify the difference in result between the two, and maybe some advantages of one, I would be very happy.
My Notes: I like the looks of a simple declare function, for one thing it's short and looks nice. For another, I like avoiding hard coded paths unless I know the files won't be found in different places. On the other hand, I really like the error handling in method 2.
Declare is (typically) used for calling stdcall-supported functions exported from Windows DLLs. The VB run-time takes care of calling LoadLibrary, GetProcAddress, etc. to locate and load the functions you've declared. Most of the Windows API is used this way from VB/VBA.
References are used for COM objects -- that is, libraries that define a type library and a COM interface.
It's rare for a library to support both. If what you have is a COM library (if it appears in the 'References' dialog), add a reference to it and instantiate it that way.