问题
I've found how to add a reference programmatically with VBA,
This explains how to add object references programmatically using the name of the Library,
with the example "VBScript_RefExp_55".
My question is how do I find this reference name to use in this code for different object libraries?
Such as the PowerPoint Library for example?
回答1:
I use this to get the info on my references :
Private Sub ListProjectReferencesList()
Dim i As Long
Dim VBProj As Object 'VBIDE.VBProject
Dim VBComp As Object 'VBIDE.VBComponent
Set VBProj = Application.VBE.ActiveVBProject
Dim strTmp As String
On Error Resume Next
For i = 1 To VBProj.References.Count
With VBProj.References.Item(i)
Debug.Print "Description: " & .Description & vbNewLine & _
"FullPath: " & .FullPath & vbNewLine & _
"Major.Minor: " & .Major & "." & .Minor & vbNewLine & _
"Name: " & .Name & vbNewLine & _
"GUID: " & .GUID & vbNewLine & _
"Type: " & .Type
Debug.Print "-------------------"
End With 'VBProj.References.Item(i)
Next i
End Sub
And generally, I prefer to add it with GUID rather than name.
But as pointed out by @Rory,
you should use Late Binding rather than adding References programmatically!
Why?
Because in order to add them programmatically, your users will have to go into :
- Options of the Application (Excel, ...) from which it's launched
- Trust Center
- Trust Center Settings
- Macro Settings tab
- Tick Trust access to the VBA project object model check box
- OK
- OK
So you'd better finish your code with references, then :
- Remove the references
- Change all declarations using those librairies to
Dim ??? As Object
- Check if you have
Option Explicit
at the top of the module (add it if not) - Look for app-specific variables (
Option Explicit
should throw an message on them) - Test your code a lot
- Export module to be used by others!
来源:https://stackoverflow.com/questions/43137715/finding-the-vbide-reference-name-of-object-libraries-vba