使用 VBA 代码(宏)连接参考(工具>参考)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10730300/
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-08 13:12:30  来源:igfitidea点击:

Connect references (Tools>References) with VBA code (macros)

vbareferenceconnect

提问by Abduhafiz

I want to programmatically connect some references to my VBA project using VBA code, i.e. without manually setting references using Tools>References. Is this possible? For example Microsoft office 12.0 Object library.

我想使用 VBA 代码以编程方式将一些引用连接到我的 VBA 项目,即无需使用工具>引用手动设置引用。这可能吗?例如 Microsoft Office 12.0 对象库。

回答by Fionnuala

You do not mention an Office application. In MS Access, you can use:

您没有提及 Office 应用程序。在 MS Access 中,您可以使用:

ReferenceFromFile "C:\Program Files\Common Files\Microsoft Shared\OFFICE14\MSO.DLL"

That is, give the full path for the reference you wish to add.

也就是说,为您要添加的参考提供完整路径。

From: http://wiki.lessthandot.com/index.php/Add,_Remove,_Check_References

来自:http: //wiki.lessthandot.com/index.php/Add,_Remove,_Check_References

Similarly, in Excel:

同样,在 Excel 中:

ActiveWorkbook.VBProject.References.AddFromFile "C:\Program Files\Common Files\Microsoft Shared\OFFICE14\MSO.DLL"

To list references in Excel: Dim ref As Reference

在 Excel 中列出引用: Dim ref As Reference

For Each ref In ActiveWorkbook.VBProject.References
    Debug.Print ref.Description; " -- "; ref.FullPath
Next

This returns the following on my machine on one particular workbook:

这将在我的机器上的一个特定工作簿上返回以下内容:

Visual Basic For Applications -- C:\PROGRA~1\COMMON~1\MICROS~1\VBA\VBA7\VBE7.DLL
Microsoft Excel 14.0 Object Library -- C:\Program Files\Microsoft Office\Office14\EXCEL.EXE
OLE Automation -- C:\Windows\system32\stdole2.tlb
Microsoft Forms 2.0 Object Library -- C:\Windows\system32\FM20.DLL
Microsoft ActiveX Data Objects 6.0 Library -- C:\Program Files\Common Files\System\ado\msado15.dll
Microsoft XML, v6.0 -- C:\Windows\System32\msxml6.dll
Microsoft Office 14.0 Access database engine Object Library -- C:\Program Files\Common Files\Microsoft Shared\OFFICE14\ACEDAO.DLL
Microsoft Visual Basic for Applications Extensibility 5.3 -- C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB
Microsoft Office 14.0 Object Library -- C:\Program Files\Common Files\Microsoft Shared\OFFICE14\MSO.DLL

回答by Robert Hind

Making references to the full path is fraught with danger and will cause you problems when the code is used on different computers or in different versions.

引用完整路径充满危险,当代码在不同的计算机或不同的版本中使用时,会给您带来问题。

Far better to use the GUID which in most (but not all instances) remains the same for different versions of an application.

最好使用 GUID,它在大多数(但不是所有实例)中对于不同版本的应用程序保持相同。

This is helpful especially if you've written code for say Excel 2010 that needs to be used in both Excel 2010 and Excel 2013.

这特别有用,如果您已经为 Excel 2010 编写了需要在 Excel 2010 和 Excel 2013 中使用的代码。

回答by Tinker

There is an alternate location where the MSO.dll file can be found:

可以在另一个位置找到 MSO.dll 文件:

C:\Program Files (x86)\Common Files\microsoft shared\oFFICE11

C:\Program Files (x86)\Common Files\microsoft shared\oFFICE11

Just set the Tool > References >Browse to the above path and locate MSO.dll

只需将 Tool > References >Browse 设置为上述路径并找到 MSO.dll