vbavstoribbonvisiocomaddin

How to Access Custom Add-In Ribbon Check Box in VBA?


I've spent 2 days now trying and searching and nothing seems to be working...

I created a custom ribbon Add-In for Visio in VSTO that installs and buttons work fine. I just recently added a couple of checkboxes to the ribbon whose states I want to read from within a VBA project.

I can't for the life of me figure out how to access the checkbox state in VBA. I tried a bunch of things with CommandBars and ToolBars but got nowhere and then I found this walkthrough which seemed promising and followed it to make the Add-In's methods visible to VBA: https://msdn.microsoft.com/en-us/library/bb608614

The VBA code does recognize the add-in and I assign the add-in object but when I try to call the object's function (getIOPressedState which refers to the state of one of the checkboxes), I get "object doesn't support this property or method".

Am I missing something here??

This is my ribbon class I want to make visible

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

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

Solution

  • I think the problem has nothing to do with checkboxes, the point is VBA by default returns you the default object interface (which in your code is NOT the IAddInUtilities). Just swap the interfaces. The IAddInUtilities should be default (first). Or remove IAddInUtilities at all, along with fancy COM stuff like ClassInterface(ClassInterfaceType.None) which is considered harmful :) Anyways, the easiest may be:

    Implements IAddInUtilities, Office.IRibbonExtensibility