How to add a reference programmatically

前端 未结 4 1865
-上瘾入骨i
-上瘾入骨i 2020-11-22 06:02

I\'ve written a program that runs and messages Skype with information when if finishes. I need to add a reference for Skype4COM.dll in order to

4条回答
  •  忘了有多久
    2020-11-22 06:54

    Here is how to get the Guid's programmatically! You can then use these guids/filepaths with an above answer to add the reference!

    Reference: http://www.vbaexpress.com/kb/getarticle.php?kb_id=278

    Sub ListReferencePaths()
    'Lists path and GUID (Globally Unique Identifier) for each referenced library.
    'Select a reference in Tools > References, then run this code to get GUID etc.
        Dim rw As Long, ref
        With ThisWorkbook.Sheets(1)
            .Cells.Clear
            rw = 1
            .Range("A" & rw & ":D" & rw) = Array("Reference","Version","GUID","Path")
            For Each ref In ThisWorkbook.VBProject.References
                rw = rw + 1
                .Range("A" & rw & ":D" & rw) = Array(ref.Description, _
                       "v." & ref.Major & "." & ref.Minor, ref.GUID, ref.FullPath)
            Next ref
            .Range("A:D").Columns.AutoFit
        End With
    End Sub
    

    Here is the same code but printing to the terminal if you don't want to dedicate a worksheet to the output.

    Sub ListReferencePaths() 
     'Macro purpose:  To determine full path and Globally Unique Identifier (GUID)
     'to each referenced library.  Select the reference in the Tools\References
     'window, then run this code to get the information on the reference's library
    
    On Error Resume Next 
    Dim i As Long 
    
    Debug.Print "Reference name" & " | " & "Full path to reference" & " | " & "Reference GUID" 
    
    For i = 1 To ThisWorkbook.VBProject.References.Count 
      With ThisWorkbook.VBProject.References(i) 
        Debug.Print .Name & " | " & .FullPath  & " | " & .GUID 
      End With 
    Next i 
    On Error GoTo 0 
    End Sub 
    

提交回复
热议问题