0

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?

4

3 回答 3

1

您不需要使用VBE.VBProjectsor Application.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 可扩展性对象模型的访问

于 2010-06-23T08:18:47.250 回答
1

您可以检查Addins集合;

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

或者您可以检查文件名

For Each Item In AddIns
   Debug.? Item.Path, Item.Name
Next
于 2010-06-22T11:01:47.990 回答
0

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

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

于 2010-06-22T15:17:16.573 回答