Finding the VBIDE.Reference.Name of object libraries vba

為{幸葍}努か 提交于 2019-12-24 09:48:21

问题


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 :

  1. Options of the Application (Excel, ...) from which it's launched
  2. Trust Center
  3. Trust Center Settings
  4. Macro Settings tab
  5. Tick Trust access to the VBA project object model check box
  6. OK
  7. OK

So you'd better finish your code with references, then :

  1. Remove the references
  2. Change all declarations using those librairies to Dim ??? As Object
  3. Check if you have Option Explicit at the top of the module (add it if not)
  4. Look for app-specific variables (Option Explicit should throw an message on them)
  5. Test your code a lot
  6. Export module to be used by others!


来源:https://stackoverflow.com/questions/43137715/finding-the-vbide-reference-name-of-object-libraries-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!