PerlBatch
PerlBatch

Reputation: 210

Enable COM addins in Excel through VBA

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

Upvotes: 3

Views: 12063

Answers (3)

canoninsea
canoninsea

Reputation: 1

I have the same system block mentioned earlier where system privileges won't allow me to use Application.COMAddIns(Name).Connect = True. This is a bit of a workaround, but to get the COM add ins box to pop up you can use SendKeys to pull it up. Keep in mind that SendKeys only excutes at the end of a run on Excel 2010 onwards, so to get it working correctly you would need to check if the user is connected to the add-in first thing. If so, call another sub; if not use SendKeys to get the dialog open and end the sub. These are the keystrokes that worked for me, there may need to be some edits depending on how many options are in your menus.

Sub test()

'Checks if COM is installed and active
comFound = False
comRibbon = True
For i = 1 To Application.COMAddIns.Count
    If Application.COMAddIns(i).Description = "NAME" Then
        comFound = True
        If Application.COMAddIns(i).Connect = False Then
            comRibbon = False
        End If
        Exit For
    End If
Next i

'Exits sub if not installed
If comFound = False Then
    MsgBox ("You do not have NAME installed.")
    Exit Sub
End If

'Directs user to rest of code if active, otherwise opens dialog
If comRibbon = True Then
    Call test2
Else
    MsgBox ("Please select NAME in the following dialog before rerunning.")
End If

SendKeys "%FT{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{TAB}{TAB}{TAB}{DOWN}{DOWN}{TAB}~", True

End Sub

Sub test2()
    'Rest of code
End Sub

Upvotes: 0

Colm Bhandal
Colm Bhandal

Reputation: 3831

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

Upvotes: 2

Excel Developers
Excel Developers

Reputation: 2825

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

Upvotes: 7

Related Questions