Enable COM addins in Excel through VBA

后端 未结 3 833
面向向阳花
面向向阳花 2020-12-21 14:14

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 O         


        
相关标签:
3条回答
  • 2020-12-21 15:03

    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
    
    0 讨论(0)
  • 2020-12-21 15:07
    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
    
    0 讨论(0)
  • 2020-12-21 15:13

    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
    
    0 讨论(0)
提交回复
热议问题