Excel VBA Checking if Addin Is Installed But Not O

2019-07-28 22:32发布

I have the following code to check if a required addin is installed/available before calling scripts within that Addin from the current context:

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

The problem arises when:

myAddin.IsOpen = false I had to add this logic to reinstall the addin. It's a slight nuisance/slow down to uninstall and reinstall the addin. Is there a way to force and Addin to "open" without re-installing the addin?

2条回答
smile是对你的礼貌
2楼-- · 2019-07-28 22:56

Per @Charles Williams

This is the method I've used:

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
查看更多
三岁会撩人
3楼-- · 2019-07-28 23:08

A simpler way is to check if the addin exists in the workbooks collection.

If it does not you can open it as though it was a workbook assuming you know the path.

(don't need to bother with the addins collection)

查看更多
登录 后发表回答