如何访问 VBA 中的自定义加载项功能区复选框?
How to Access Custom Add-In Ribbon Check Box in VBA?
我已经花了 2 天时间进行尝试和搜索,但似乎没有任何效果...
我在 VSTO 中为 Visio 创建了自定义功能区加载项,安装后按钮工作正常。我最近刚刚在功能区中添加了几个复选框,我想从 VBA 项目中读取它们的状态。
我一辈子都想不出如何访问 VBA 中的复选框状态。我用 CommandBars 和 ToolBars 尝试了很多东西,但一无所获,然后我发现这个演练看起来很有希望,并按照它使插件的方法对 VBA 可见:https://msdn.microsoft.com/en-us/library/bb608614
VBA 代码确实识别了加载项,并且我分配了加载项对象,但是当我尝试调用该对象的函数(getIOPressedState,它指的是其中一个复选框的状态)时,我得到 "object doesn't support this property or method".
我是不是遗漏了什么??
这是我的丝带class我想让它可见
<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
我认为问题与复选框无关,重点是 VBA 默认情况下 returns 您是默认对象接口(在您的代码中不是 IAddInUtilities)。只是交换接口。 IAddInUtilities 应该是默认的(第一个)。或者完全删除 IAddInUtilities,以及像 ClassInterface(ClassInterfaceType.None) 这样被认为有害的花哨的 COM 东西 :) 不管怎样,最简单的可能是:
Implements IAddInUtilities, Office.IRibbonExtensibility
我已经花了 2 天时间进行尝试和搜索,但似乎没有任何效果...
我在 VSTO 中为 Visio 创建了自定义功能区加载项,安装后按钮工作正常。我最近刚刚在功能区中添加了几个复选框,我想从 VBA 项目中读取它们的状态。
我一辈子都想不出如何访问 VBA 中的复选框状态。我用 CommandBars 和 ToolBars 尝试了很多东西,但一无所获,然后我发现这个演练看起来很有希望,并按照它使插件的方法对 VBA 可见:https://msdn.microsoft.com/en-us/library/bb608614
VBA 代码确实识别了加载项,并且我分配了加载项对象,但是当我尝试调用该对象的函数(getIOPressedState,它指的是其中一个复选框的状态)时,我得到 "object doesn't support this property or method".
我是不是遗漏了什么??
这是我的丝带class我想让它可见
<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
我认为问题与复选框无关,重点是 VBA 默认情况下 returns 您是默认对象接口(在您的代码中不是 IAddInUtilities)。只是交换接口。 IAddInUtilities 应该是默认的(第一个)。或者完全删除 IAddInUtilities,以及像 ClassInterface(ClassInterfaceType.None) 这样被认为有害的花哨的 COM 东西 :) 不管怎样,最简单的可能是:
Implements IAddInUtilities, Office.IRibbonExtensibility