PDA

View Full Version : add/fix vba reference



pandemar
06-22-2015, 10:51 AM
Hi,
trying to fix library ref error with vba.
code won't run as long as there is a "missing" reference.
using code "addreference()" from this site.

need to turn something off so that on open
the code will run, find the offending missing library
delete the reference to the library
add the reference to the good lib
and continue.

otherwise user has to go to vba and fix manually.

and way this can be done???

thanks

Kenneth Hobs
06-22-2015, 11:01 AM
No. That is why some just use late bound objects. It is best to figure out which one is causing the problem and then use the late bound method, when possible.

pandemar
06-22-2015, 11:11 AM
Hi,
thanks for the quick reply.

the user is using excel 7, the new code is 13. The reference is to outlook 15 which needs to be changed to outlook 12 for this user.
don't know how to do this with "late bound method".

peter

Kenneth Hobs
06-22-2015, 11:28 AM
You can look at some MVP sites for some examples.

I like Ron de Bruin's since this is an Excel forum. http://www.rondebruin.nl/win/s1/outlook/mail.htm

See the bottom of his link below there where his previous code uses CreateObject("Outlook.Application") for the late bound method and early bound with the needed reference example at the end. Note that late bound used Dim somevariable as Object whereas early bound Dims must use the proper object dimensional types.

http://www.rondebruin.nl/win/s1/outlook/signature.htm

pandemar
06-22-2015, 11:37 AM
thank you so much
if i understand correctly...
with late binding your code doesn't rely on the reference in tools. so uncheck the box and let the code do the work.

thanks again
peter
(makes you wonder why the code I was using was ever posted if it couldn't work???)

Kenneth Hobs
06-22-2015, 12:31 PM
No, there is more to it than just removing a reference. What some do is to code using early bound methods but make the production version late bound.

See the links here if you want to learn more. http://lmgtfy.com/?q=vba+late+binding+vs+early+binding

Here is a quick example where I show how to use the Scripting.FileSystemObject early bound method method and the late bound method is commented out.

Sub CopyFileToDir(filespec, Destination)
'Late bound
'dim fs as Object
'Set fs = CreateObject("Scripting.FileSystemObject")

'Early bound
'Needs Reference: MicroSoft Script Runtime, scrrun.dll
'Instructions: http://support.microsoft.com/default.aspx?scid=kb;en-us;186118
Dim fs As Scripting.FileSystemObject
Set fs = New FileSystemObject


fs.CopyFolder ThisWorkbook.path, "t:\", True
fs.CopyFile filespec, Destination
End Sub