问题
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?
回答1:
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)
回答2:
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
来源:https://stackoverflow.com/questions/39413928/excel-vba-checking-if-addin-is-installed-but-not-open