Link to home
Start Free TrialLog in
Avatar of Paul Cook-Giles
Paul Cook-GilesFlag for United States of America

asked on

Need to set reference with Excel VBA

I need to set a reference to a library (Microsoft ActiveX Data Objects 2.6 Library) with VBA.  I've tried
Application.References.AddFromFile "C:\Program Files (x86)\Common Files\System\ado\msado.tlb"

Open in new window


and
With Excel.Application
    .AddFromFile "C:\Program Files (x86)\Common Files\System\ado\msado.tlb"
End With

Open in new window


Neither of them work.  Any suggestions?
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Why do you need to set a reference in code?
Avatar of Paul Cook-Giles

ASKER

Because I'm releasing a function into the production environment that requires it, and I don't want to try coaching 55 people through setting the reference manually.  :)
I don't follow. Why would they need to set the reference themselves? It's saved with the workbook containing the function.
Hm.  Then maybe I'm asking the wrong question (this is the first time I've written an Excel function).  

Maybe I should be asking "What's the best way to distribute a new Excel function, which requires a reference (to Microsoft ActiveX Data Objects 2.6 Library),  to a non-technical user group?"  Ideally, they would have access to the new function in every spreadsheet they edit, new or pre-existing.

Paul
Avatar of Norie
Norie

What does the code you want to distribute do?

What references does it require?

Have you considered using late binding?

PS Are the people who will be using this code all on the same version(s) of Excel/Office etc?
ASKER CERTIFIED SOLUTION
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Don't use references.  Use late binding via the CreateObject() function.
I do my development with References set.

Then replace the object 'set xx = new yy' lines with CreateObject as aikimark notes.  Then test again to make sure it works.
I have a workbook I distribute to our group all the time.  That workbook happens to use the reference you mentioned, as well as several others.

When one of my team members picks up that workbook, they do not have to set references, it is already part of the workbook. -- I think this is the same thing that Rory mentioned above.
Thanks, everyone!  Rory, I think you may have provided the solution;  let me test it out and I'll be back to update this question.  (Note that the url for your first link should be http://excel-it.com/excel-addins.html)

PCG
Thanks for correcting the link but my name is Roy, Rory is another member.