VBA:检测库在运行时是否可用

发布于 2024-09-06 10:10:52 字数 326 浏览 3 评论 0 原文

我有一个 xla 文件,它引用了目标计算机上可能存在或不存在的一些其他 Excel 加载项。目前,Excel 无法加载 xla,并显示“编译错误:找不到项目或库”。

代码类似于:

  Dim result as Integer
  result = SomeAddIn.SomeFunction(x)

在其前面加上“On Error Goto AddInMissing”前缀并没有帮助,因为这被视为编译错误。

那么,是否有任何方法可以在 VBA 中按名称进行后期绑定/引用加载项,以便在某个加载项不存在时我可以优雅地失败(禁用某些功能)?

I have an xla file that refers to some other Excel add-ins that may or may not be present on the target machine. Currently Excel fails on loading the xla with a "Compile error: Can't find project or library".

Code is something like:

  Dim result as Integer
  result = SomeAddIn.SomeFunction(x)

Prefixing this with an "On Error Goto AddInMissing" doesn't help as this is seen as a compile error.

So, is there any way of late-binding/referencing an add-in by name in VBA so that I can fail gracefully (disable certain features) if a certain add-in is not present?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事灯 2024-09-13 10:10:52

您可以检查 Addins 集合;

if AddIns("The addins title").Installed = True then ...

或者您可以检查文件名

For Each Item In AddIns
   Debug.? Item.Path, Item.Name
Next

You can examine the Addins Collection;

if AddIns("The addins title").Installed = True then ...

or you can examine file names

For Each Item In AddIns
   Debug.? Item.Path, Item.Name
Next
删除→记忆 2024-09-13 10:10:52

您不需要使用 VBE.VBProjectsApplication.AddIns:XLA 是 Workbooks 集合中的隐藏元素,因此如果您知道XLA 加载项的名称,您可以直接在 Workbooks 集合中检查它是否打开:

Public Function IsLoaded(ByVal AddInName As String) As Boolean
    On Error Resume Next
    Dim xla As Object
    Set xla = Application.Workbooks(AddInName)
    IsLoaded = Not xla Is Nothing
    On Error GoTo 0
End Function

这样您就不需要用户系统信任对 VBE Extensibility 对象的访问模型。

You don't need to use VBE.VBProjects or Application.AddIns: an XLA is a hidden element in the Workbooks collection so if you know the name of the XLA add-in you can directly check in the Workbooks collection to see if it is open:

Public Function IsLoaded(ByVal AddInName As String) As Boolean
    On Error Resume Next
    Dim xla As Object
    Set xla = Application.Workbooks(AddInName)
    IsLoaded = Not xla Is Nothing
    On Error GoTo 0
End Function

This way you do not need the user system to trust access to the VBE Extensibility object model.

鹿港巷口少年归 2024-09-13 10:10:52

谢谢大家。迭代 Application.VBE.VBProjects 允许我检测 xla 是否存在。下一步是将目标 xla 中定义的函数的所有调用包装在包装器模块中。这是必要的,因为我试图避免出现编译错误。我仅在 xla 可用时调用这些包装器函数,幸运的是,VBA 在需要模块之前不会编译模块(这基本上意味着创建两个包装器 - 外部包装器检查 xla 是否可用,内部包装器调用目标 xla)。

更复杂的解决方案可能会同时考虑 AddIns 和 VBE.VBEProjects

Thanks everyone. Iterating through Application.VBE.VBProjects allows me to detect if the xla is present. The next step was to wrap all calls to the functions defined in the target xla in a wrapper module. This was necessary as I'm trying to avoid getting compile errors. I only call these wrapper functions if the xla is available and fortunately VBA doesn't compile modules until they are needed (this basically meant creating two wrappers - the outer wrapper checking if the xla was available and the inner one calling the target xla).

A more sophisticated solution would probably look at both AddIns and VBE.VBEProjects

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文