Page 1 of 1
VBA reference library
Posted: Mon Oct 06, 2014 5:15 am
by dan.solverud@gov.se
Since not all users on the office LAN have EViews installed I need to use VBA to activate and deactivate the EViews 8.0 Type Library. While the activation goes splendid, the deactivation of the reference library is failing. To activate I use the command
vbProj.References.AddFromFile "C:\Program Files (x86)\EViews 8\x64\EViewsMgr.dll\1"
But the corresponding reference will not work in the deactivation command, i.e.:
vbProj.References.Remove "C:\Program Files (x86)\EViews 8\x64\EViewsMgr.dll\1"
How should I refer to the EViews 8.0 Type Library in the deactivation command?
Re: VBA reference library
Posted: Mon Oct 06, 2014 8:01 am
by EViews Steve
I had no idea you could even do this dynamically. You have to allow trusted access to VBA project object model under "Macro Settings" for this to even work. I had to google the Remove method. It doesn't take a string path to the dll -- it takes a Reference object. The following code sample works on my machine...
Code: Select all
Public Sub test()
On Error GoTo ErrorHandler
Dim ref
Set ref = Application.VBE.ActiveVBProject.References.AddFromFile("C:\Program Files\EViews 8\EViewsMgr.dll")
MsgBox "reference added"
Application.VBE.ActiveVBProject.References.Remove ref
MsgBox "reference removed"
ExitHandler:
Exit Sub
ErrorHandler:
MsgBox Err.Description, vbOKOnly, "Error"
Resume ExitHandler
End Sub
Re: VBA reference library
Posted: Mon Oct 06, 2014 10:18 am
by dan.solverud@gov.se
Thank you, your code sample works on my machine as well. I tried to solve the tasks (adding and removing) in different sub's, which I could not get to work, but if I follow your example to put both the adding and the removing command in the same sub it works just fine.
Re: VBA reference library
Posted: Mon Oct 06, 2014 10:58 am
by administrator
You can place the "ref" variable outside the method so it will be available for different methods.
Or, on exit, you can loop thru all references and see if any match the one that says "EViews 8.0 Type Library". Once found, you can remove that one.
Steve