如何以编程方式添加对 VBA 项目的引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3837720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:07:57  来源:igfitidea点击:

How do I programatically add a reference to a VBA project?

vba

提问by GeneQ

I'm deploying an early bound styled VBA module that needs Scripting.Dictionaryand RegExp.

我正在部署一个需要Scripting.DictionaryRegExp的早期绑定样式的 VBA 模块。

The script, predictably, fails when it runs on another computer. The user has to go to Tools->Reference in the VBA IDE and add a reference to those two libraries manually to make it work.

可以预见,该脚本在另一台计算机上运行时会失败。用户必须在 VBA IDE 中转到 Tools->Reference 并手动添加对这两个库的引用以使其工作。

Hence lies the problem. Asking the non-technical end user to go to the IDE and manually add references is asking way too much of them.

问题就在于此。要求非技术最终用户转到 IDE 并手动添加引用是对他们的要求太多了。

The other alternative is to rewrite the whole (very long script written by someone else) to use late binding. I rather not take this path if there are other methods.

另一种选择是重写整个(由其他人编写的很长的脚本)以使用后期绑定。如果有其他方法,我宁愿不走这条路。

As an altervative, some people suggest adding a reference programatically like so:

作为替代方案,有些人建议像这样以编程方式添加参考:

Application.VBE.ActiveVBProject.References.AddFromFile [Path to library]

Application.VBE.ActiveVBProject.References.AddFromFile [Path to library]

  1. Is this the correct solution and if so are there any downsides of this strategy?
  2. If not, are there other methods that will to enable the code to remain early bound yet does not require references to be added manually by the user.
  1. 这是正确的解决方案吗?如果是,这种策略有什么缺点吗?
  2. 如果没有,是否有其他方法可以使代码保持早期绑定,但不需要用户手动添加引用。

Suggestions involving direct calls to the Win32/64 API are also welcome.

也欢迎直接调用 Win32/64 API 的建议。

Thanks.

谢谢。

采纳答案by jtolle

In my own limited environment (small # of other people using spreadsheets I develop, relatively standard machine setups), if I create the file and add the references, and then give a copy to someone else, they can open it with no problems and not have to do anything, so keep that in mind with this answer. (I'm wondering why that doesn't work for you.) Also, this was with Excel.

在我自己有限的环境中(使用我开发的电子表格的少数人,相对标准的机器设置),如果我创建文件并添加引用,然后将副本提供给其他人,他们可以毫无问题地打开它,而不是必须做任何事情,所以请记住这个答案。(我想知道为什么这对您不起作用。)另外,这是使用 Excel。

Rather than adding a reference from a file path, you might consider using the GUID property instead.

您可以考虑使用 GUID 属性,而不是从文件路径添加引用。

Here is some code I once used to automatically create references in a newly created workbook. (It's part of a script that would export code, references, and unit tests on worksheets to text for use with Subversion and then later reconstitute the workbook from the text files.) You might find it useful to your situation. (EH and cleanup removed to keep it short...)

这是我曾经用来在新创建的工作簿中自动创建引用的一些代码。(它是脚本的一部分,可将工作表上的代码、引用和单元测试导出为文本以供 Subversion 使用,然后稍后从文本文件重新构建工作簿。)您可能会发现它对您的情况很有用。(EH 和清理被删除以保持简短......)

'Export refs in existing workbook to text file
Private Sub exportRefs_(srcWbk As Workbook)
    Dim fs As FileSystemObject
    Set fs = New FileSystemObject

    Dim tsout As TextStream
    Set tsout = fs.CreateTextFile(fs.BuildPath(getTargetPath_(srcWbk), "refs.refs"))

    Dim ref As Reference
    For Each ref In Application.ThisWorkbook.VBProject.References
        Call tsout.WriteLine(ref.GUID)
    Next ref

    '<EH + cleanup...>
End Sub


'Add refs to newly created workbook based on previously exported text file
Private Sub importRefs_(wbk As Workbook, path As String)
    Dim fs As FileSystemObject
    Set fs = New FileSystemObject

    Dim tsin As TextStream
    Set tsin = fs.OpenTextFile(path)

    Dim line As String
    Dim ref As Reference

    While Not tsin.AtEndOfStream
        line = tsin.ReadLine()

        Set ref = Nothing

        On Error Resume Next
            Set ref = wbk.VBProject.References.AddFromGuid(line, 0, 0)
        On Error GoTo 0

        If ref Is Nothing Then
            Debug.Print "add failed: " & line
        End If
    Wend

    '<EH + cleanup...>
End Sub

Like, I said, limited environment, but hopefully it helps.

就像,我说,有限的环境,但希望它有所帮助。