问题
I need to enable COM addins through VBA. The addins already exists under COM addins, but become unchecked when Excel crashes.
Sub hyp()
Dim objAddIn As Object
For i = 1 To Application.COMAddIns.Count
Set objAddIn = Application.COMAddIns.Item(i)
On Error Resume Next
If Application.COMAddIns.Item(i).Description = "Oracle Smart View for Office" Then
'MsgBox Application.COMAddIns.Item(i).Description
'NEED TO ENABLE THE COM ADDIN
Else
End If
Next i
End Sub
回答1:
Public Sub Connect_COM_AddIn(Name As String)
Dim ndx As Integer
For ndx = 1 To Application.COMAddIns.Count
If Application.COMAddIns(ndx).Description = Name Then
Application.COMAddIns(ndx).Connect = True
Exit For
End If
Next
End Sub
回答2:
Note: Please see the comment of BigBen below - this approach may not always work as the indexer does not always coincide with the description. If you need to search by description, then the Excel Developers answer is probably applicable (though I haven't personally tried it or needed it).
A simpler alternative to the answer of Excel Developers that worked for me is to index the com add in directly by its string name instead of looping through the com add ins using an integer index and comparing to the description. In particular, this code worked for me (I've included a connect and disconnect version):
Public Sub Connect_COM_AddIn(Name As String)
Application.COMAddIns(Name).Connect = True
End Sub
Public Sub Disconnect_COM_AddIn(Name As String)
Application.COMAddIns(Name).Connect = False
End Sub
来源:https://stackoverflow.com/questions/48565694/enable-com-addins-in-excel-through-vba