Excel VBA检查Addin是否已安装但未打开

时间:2016-09-09 14:27:44

标签: excel vba excel-vba

我有以下代码来检查在从当前上下文调用该Addin中的脚本之前是否安装了所需的插件:

Function IsAddinEnabled(addinName as string) As Boolean
    IsAddinEnabled = True
    Dim myAddin As addin
    On Error GoTo NotExists
    Set myAddin = Application.AddIns2(addinName)
    If myAddin.IsOpen = False Then ' this logic is my workaround
        myAddin.Installed = False 'uninstall 
        myAddin.Installed = True ' install to "Open" the addin
    Else
        myAddin.Installed = True 'redundant
    End If
    Exit Function
NotExists:
    IsAddinEnabled = False
End Function

问题出现在:

myAddin.IsOpen = false我必须添加此逻辑才能重新安装插件。卸载并重新安装插件有点麻烦/慢。有没有办法强制和Addin“打开”而无需重新安装插件?

2 个答案:

答案 0 :(得分:1)

更简单的方法是检查工作簿集合中是否存在插件。

如果没有,您可以打开它,就好像它是一个工作簿,假设您知道路径。

(不需要打扰addins集合)

答案 1 :(得分:0)

Per @Charles Williams

这是我使用的方法:

Function IsAddinEnabled(addinName as string) As Boolean
    IsAddinEnabled = True
    Dim myAddin As addin
    On Error GoTo NotExists
    Set myAddin = Application.AddIns2(addinName)
    If myAddin.IsOpen = False Then ' this logic is my workaround
        Workbooks.Open myAddin.Path & "\" & myAddin.Name 'open the addin, if it's not open
    Else
        myAddin.Installed = True 'redundant
    End If
    Exit Function
NotExists:
    IsAddinEnabled = False
End Function