如何在VBA中访问自定义加载项功能区复选框?

时间:2016-04-08 18:10:26

标签: vba vsto ribbon visio comaddin

我现在花了两天时间尝试和搜索,似乎没有任何工作......

我在VSTO中为Visio创建了一个自定义功能区加载项,安装和按钮工作正常。我刚刚在功能区中添加了几个复选框,其状态我想在VBA项目中读取。

我不能为我的生活弄清楚如何在VBA中访问复选框状态。我尝试了一些使用CommandBars和ToolBars的东西,但无处可去,然后我发现这个演示看起来很有希望并遵循它使VBA可以看到加载项的方法:https://msdn.microsoft.com/en-us/library/bb608614

VBA代码确实识别了加载项并且我分配了加载项对象,但是当我尝试调用对象的函数(getIOPressedState引用其中一个复选框的状态)时,我得到“对象没有支持此属性或方法“。

我在这里遗漏了什么吗?

这是我想要显示的功能区类

<ComVisible(True)> _
Public Interface IAddInUtilities
    Function getIOPressed() As Boolean
    Function getDDPressed() As Boolean
    Sub doNothing()
End Interface

<Runtime.InteropServices.ComVisible(True)> _
<ClassInterface(ClassInterfaceType.None)> _
Public Class StructuredAnalysisRibbon
Implements Office.IRibbonExtensibility, IAddInUtilities
    Public ioPressedState As Boolean = False
    Public ddPressedState As Boolean = False
    Public ribbon As Office.IRibbonUI

    Public Function GetCustomUI(ByVal ribbonID As String) As String Implements   Office.IRibbonExtensibility.GetCustomUI
        Return getResourceText("SAVisioAddIn.StructuredAnalysisRibbon.xml")
    End Function

    Public Function getIOPressed() As Boolean Implements IAddInUtilities.getIOPressed
        Return ioPressedState
    End Function

    Public Function getDDPressed() As Boolean Implements IAddInUtilities.getDDPressed
        Return ddPressedState
    End Function

    Public Sub doNothing() Implements IAddInUtilities.doNothing
        'do nothing-added this to see if function As boolean in interface was causing issues
    End Sub

ThisAddIn.vb

Public SARibbon As StructuredAnalysisRibbon
Protected Overrides Function CreateRibbonExtensibilityObject() As    Microsoft.Office.Core.IRibbonExtensibility
    Return SARibbon
End Function

Protected Overrides Function RequestComAddInAutomationService() As Object
    If SARibbon Is Nothing Then
        SARibbon = New StructuredAnalysisRibbon
    End If
    Return SARibbon
End Function

Visio VBA代码

Public Sub bloop()
    Dim addIn As COMAddIn
    Dim addInObject As Object
    Dim ioPressed As Boolean
    ioPressed = False
    Set addIn = Application.COMAddIns.Item("SAVisioAddIn")
    Set addInObject = addIn.Object

    ioPressed = addInObject.getIOPressed   'fails here bc method not recognized for object
    'Also tried addIn.Object.doNothing and still didn't work
    If ioPressed = True Then
        MsgBox "checked"
    Else
        MsgBox "not checked"
    End If
End Sub

1 个答案:

答案 0 :(得分:2)

我认为问题与复选框无关,关键是VBA默认返回默认对象接口(在代码中不是IAddInUtilities)。只需交换接口即可。 IAddInUtilities应该是默认的(第一个)。或者删除IAddInUtilities,以及像ClassInterface(ClassInterfaceType.None)这样被认为有害的奇特COM​​东西:)无论如何,最简单的可能是:

Implements IAddInUtilities, Office.IRibbonExtensibility