如何添加引用编程

问题描述 投票:76回答:4

我已经写在运行,如果完成时与信息的消息的Skype程序。我需要为了通过Skype发送消息添加对Skype4COM.dll参考。我们有一个网络上的十几个电脑共享文件服务器(除其他事项外)。所有其他电脑需要能够运行这个程序。我希望避免设立由专人参考。我原计划投入的共享位置的参考,并将其添加程序,当程序运行。

我似乎无法弄清楚如何使用VBA编程方式添加到Excel 2007中的引用。我知道该怎么做手工:打开VBE --> Tools --> References --> browse --_> File Location and Name。但是,这不是我的目的是非常有用的。我知道有很多方法可以做到这一点的Access Vb.net和与此类似不停弹出的代码,但我不知道我的理解,或者是相关的:

ThisWorkbook.VBProject.References.AddFromGuid _
    GUID:="{0002E157-0000-0000-C000-000000000046}", _
    Major:=5, Minor:=3

到目前为止,在解决方案提出,为了添加引用编程我需要添加手工参考,并更改信任中心 - 这不仅仅是增加了更多的参考。虽然我想如果我坚持完成提出的解决方案,我将能够以编程方式添加将来参考。这可能使得它值得的。

任何进一步的想法将是巨大的。

excel excel-vba reference vba
4个回答
100
投票

ommit

有两种方法可以通过VBA添加到您的项目中引用

1)使用GUID

2)直接引用该DLL。

让我来涵盖。

但首先这些都是三件事情你需要照顾

)应被启用宏

B)在安全设置,确保“信任访问到Visual Basic项目”被选中

c)你手动设置的应用程序扩展到`的Microsoft Visual Basic参考”对象

方法1(使用GUID)

我通常会避免这种方式,因为我有搜索的GUID在注册表......我恨LOL。更多关于GUID here

主题:通过代码添加VBA参考图书馆

链接:http://www.vbaexpress.com/kb/getarticle.php?kb_id=267

'Credits: Ken Puls
Sub AddReference()
     'Macro purpose:  To add a reference to the project using the GUID for the
     'reference library

    Dim strGUID As String, theRef As Variant, i As Long

     'Update the GUID you need below.
    strGUID = "{00020905-0000-0000-C000-000000000046}"

     'Set to continue in case of error
    On Error Resume Next

     'Remove any missing references
    For i = ThisWorkbook.VBProject.References.Count To 1 Step -1
        Set theRef = ThisWorkbook.VBProject.References.Item(i)
        If theRef.isbroken = True Then
            ThisWorkbook.VBProject.References.Remove theRef
        End If
    Next i

     'Clear any errors so that error trapping for GUID additions can be evaluated
    Err.Clear

     'Add the reference
    ThisWorkbook.VBProject.References.AddFromGuid _
    GUID:=strGUID, Major:=1, Minor:=0

     'If an error was encountered, inform the user
    Select Case Err.Number
    Case Is = 32813
         'Reference already in use.  No action necessary
    Case Is = vbNullString
         'Reference added without issue
    Case Else
         'An unknown error was encountered, so alert the user
        MsgBox "A problem was encountered trying to" & vbNewLine _
        & "add or remove a reference in this file" & vbNewLine & "Please check the " _
        & "references in your VBA project!", vbCritical + vbOKOnly, "Error!"
    End Select
    On Error GoTo 0
End Sub

方式2(直接引用该dll)

该代码添加一个参考Microsoft VBScript Regular Expressions 5.5

Option Explicit

Sub AddReference()
    Dim VBAEditor As VBIDE.VBE
    Dim vbProj As VBIDE.VBProject
    Dim chkRef As VBIDE.Reference
    Dim BoolExists As Boolean

    Set VBAEditor = Application.VBE
    Set vbProj = ActiveWorkbook.VBProject

    '~~> Check if "Microsoft VBScript Regular Expressions 5.5" is already added
    For Each chkRef In vbProj.References
        If chkRef.Name = "VBScript_RegExp_55" Then
            BoolExists = True
            GoTo CleanUp
        End If
    Next

    vbProj.References.AddFromFile "C:\WINDOWS\system32\vbscript.dll\3"

CleanUp:
    If BoolExists = True Then
        MsgBox "Reference already exists"
    Else
        MsgBox "Reference Added Successfully"
    End If

    Set vbProj = Nothing
    Set VBAEditor = Nothing
End Sub

注:我还没有添加错误处理。建议在您的实际代码,不要使用它:)

编辑由mischab1殴打:)


23
投票

有两种方法可以添加使用VBA引用。 .AddFromGuid(Guid, Major, Minor).AddFromFile(Filename)。哪一个是最好的取决于你想添加到一个参考的东西。我几乎总是使用.AddFromFile因为我引用的东西是其他Excel VBA项目,他们是不是在Windows注册表。

你是显示将添加到工作簿的代码是在参考示例代码。我一般看不到任何一点在这样做,因为90%的时间,然后才能添加引用,代码已经无法编译因为参考丢失。 (如果没没到编译,您可能正在使用后期绑定,你并不需要添加一个参考。)

如果您在获取代码运行的问题,有两种可能的问题。

  1. 为了方便地使用VBE的对象模型,你需要添加到为应用程序扩展的Microsoft Visual Basic的参考。 (VBIDE)
  2. 为了运行Excel VBA代码,在一个VBProject改变任何东西,你需要信任对VBA项目对象模型。 (在Excel 2010中,它位于信任中心 - 宏设置。)

除此之外,如果你可以在你尝试这样做,不工作你的问题还是什么更加清楚一点,我可以给一个更具体的答案。


7
投票

浏览注册表的GUID或使用的路径,该方法是最好的。如果浏览注册表不再是必要的,那会不会使用GUID的更好的办法?办公室并不总是安装在同一目录下。安装路径可以手动改变。另外,版本号是路径的一部分。我从来没有预测,微软将有史以来推出的64个处理器前添加“(86)”到“程序文件”。如果可能的话我会尽量避免使用的路径。

下面的代码是从亚洲时报Siddharth溃败的回答得出,具有附加功能,列出了在活动工作簿中使用的所有引用。如果我在版本的Excel打开工作簿我?将工作簿仍然没有适应VBA代码工作?我已经检查到Office 2003和2010的GUID是相同的。让我们希望微软不会在将来的版本更改GUID。

该参数0,0(从.AddFromGuid)应该使用最新版本的引用的(我一直无法测试)。

你怎么看?当然,我们无法预知未来,但我们能做些什么来使我们的代码版本的证明?

Sub AddReferences(wbk As Workbook)
    ' Run DebugPrintExistingRefs in the immediate pane, to show guids of existing references
    AddRef wbk, "{00025E01-0000-0000-C000-000000000046}", "DAO"
    AddRef wbk, "{00020905-0000-0000-C000-000000000046}", "Word"
    AddRef wbk, "{91493440-5A91-11CF-8700-00AA0060263B}", "PowerPoint"
End Sub

Sub AddRef(wbk As Workbook, sGuid As String, sRefName As String)
    Dim i As Integer
    On Error GoTo EH
    With wbk.VBProject.References
        For i = 1 To .Count
            If .Item(i).Name = sRefName Then
               Exit For
            End If
        Next i
        If i > .Count Then
           .AddFromGuid sGuid, 0, 0 ' 0,0 should pick the latest version installed on the computer
        End If
    End With
EX: Exit Sub
EH: MsgBox "Error in 'AddRef'" & vbCrLf & vbCrLf & err.Description
    Resume EX
    Resume ' debug code
End Sub

Public Sub DebugPrintExistingRefs()
    Dim i As Integer
    With Application.ThisWorkbook.VBProject.References
        For i = 1 To .Count
            Debug.Print "    AddRef wbk, """ & .Item(i).GUID & """, """ & .Item(i).Name & """"
        Next i
    End With
End Sub

上面的代码并不需要参考“的应用程序扩展的Microsoft Visual Basic”对象了。


5
投票

下面是如何获得的GUID的程序!然后,您可以使用这些GUID /文件路径与上述答案补充参考!

参考: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

下面是相同的代码,但打印到终端,如果你不打算将工作表的输出。

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 
© www.soinside.com 2019 - 2024. All rights reserved.