通过VBA在Excel中启用COM加载项



我需要通过VBA启用COM插件。加载项已存在于 COM 加载项下,但在 Excel 崩溃时变为未选中状态。

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
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

注意:请参阅下面 BigBen 的评论 - 这种方法可能并不总是有效,因为索引器并不总是与描述一致。如果您需要按描述进行搜索,那么 Excel 开发人员的答案可能适用(尽管我个人没有尝试或需要它(。


我有用的 Excel 开发人员的答案的更简单的替代方法是直接按其字符串名称索引 com add,而不是使用整数索引循环浏览 com addins 并与描述进行比较。特别是,这段代码对我有用(我包含一个连接和断开连接版本(:

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

我有前面提到的相同的系统块,系统权限不允许我使用Application.COMAddIns(Name(。连接 = 真。这是一种解决方法,但要弹出 COM 加载项框,您可以使用 SendKeys 将其拉出。请记住,SendKeys 仅在 Excel 2010 上运行结束时执行,因此要使其正常工作,您需要首先检查用户是否已连接到加载项。如果是这样,请呼叫另一个潜艇;如果没有,请使用发送键打开对话框并结束子。这些是对我有用的击键,可能需要进行一些编辑,具体取决于菜单中的选项数量。

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

相关内容

最新更新