Ksenia February 2016

How to add Microsoft ActiveX Data Objects 6.1 Library and Microsoft VBScript Regular Expressions 5.5 using macro code

I always added references into my .bas module through "Tools...->References...". And now I want to add references automatically, using macro code. I need to add the two references: Microsoft ActiveX Data Objects 6.1 Library and Microsoft VBScript Regular Expressions 5.5. And I found a piece of code that seems to do what I want:

Sub ListReferences()
    On Error GoTo CanNotAddReferences
    Application.VBE.ActiveVBProject.References.AddFromFile "PATH_TO_FILE"
    Exit Sub
CanNotAddReferences:
    MsgBox "Can not add reference"
End Sub

But I don't know how to get paths of files (PATH_TO_FILE). Maybe somebody can help me? Thanks in advance.

Answers


destination-data February 2016

You can find the file path on the References VBAProject dialog (Tools >> References). Look for the location property.

References VBAProject Dialog

EDIT: As pointed out in the comments, you cannot always read the full name. This sub outputs all the current references.

Sub ReadExistingRefences()
' Displays all current refernce names and full paths.
Dim ref As Object

    For Each ref In Application.VBE.ActiveVBProject.References
        MsgBox "Name: " & ref.Name & vbCrLf & ref.FullPath
    Next
End Sub

Warning

You should really read @MacroMan's comment on the OP. Although you can update references programmatically, you probably shouldn't.

Post Status

Asked in February 2016
Viewed 1,608 times
Voted 14
Answered 1 times

Search




Leave an answer