Excel功能区切换按钮未运行onAction或getPressed回调

时间:2018-01-11 16:07:40

标签: excel vba ribbonx

我已经通过Excel.CustomUI文件向功能区添加了一个切换按钮,这很好,但它实际上不会触发任何回调函数。我已经尝试过网络上给出的各种组合,但根本无法运行任何功能。

我也尝试过一个复选框,但这也没有运行功能。我检查并删除了任何安全措施,但无济于事。

我有标准按钮,可以根据需要使用。

我错过了什么?

这是excel.customUI文件

    <mso:customUI      xmlns:mso='http://schemas.microsoft.com/office/2009/07/customui' 
                       onLoad='ToggleAutoCalc' >
  <mso:ribbon>
    <mso:qat/>
    <mso:tabs>
      <mso:tab id='reportTab' label='ICit' insertBeforeQ='mso:TabFormat'>
        <mso:group id='reportGroup' label='DBRW Copy Paste' autoScale='true'>
          <mso:button id='DBRW Copy' label='DBRW Copy' 
imageMso='Copy'      onAction='DBRWCopyPaste.xlam!TM1Copy'/>
          <mso:button id='DBRW Paste' label='DBRW Paste' 
imageMso='Paste'      onAction='DBRWCopyPaste.xlam!TM1Paste'/>
          <mso:button id='DBRW UnDo' label='DBRW UnDo' 
imageMso='CellsDelete'      onAction='DBRWCopyPaste.xlam!TM1UndoPaste'/>
          **<mso:toggleButton id='AutoCalc' label='AutoCalc' 
           screentip='Toggle Autocalc'
           size='Large'
imageMso='ColumnSettingsMenu'      onAction='DBRWCopyPaste.xlam!TbtnToggleAutoCalc'
           getPressed='DBRWCopyPaste.xlam!GetPressed'/>**
        </mso:group>
      </mso:tab>
    </mso:tabs>
  </mso:ribbon>
</mso:customUI>

这是我的测试VBA代码:

Option Explicit

Public MyRibbon As IRibbonUI


Sub ToggleAutoCalc(ribbon As IRibbonUI)

Set MyRibbon = ribbon

Debug.Print "ToggleAutoCalc"

End Sub

Sub TbtnToggleAutoCalc(control As IRibbonControl, pressed As Boolean)
pressed = True
End Sub

Sub GetPressed(control As IRibbonControl, ByRef returnedVal)
Dim bAutoCalc As Integer
Dim sVal As String
Dim nVal As Integer

GetPressed = returnedVal


End Sub

1 个答案:

答案 0 :(得分:0)

单引号应该是XML中的双引号。 id标签也不允许名称有空格。

您是否使用自定义UI编辑器编辑XML?该工具中有一项功能可以检查您的XML是否格式正确。这是一个链接https://www.rondebruin.nl/win/s2/win001.htm

<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="ToggleAutoCalc">
    <ribbon>
        <tabs>
            <tab 
                    id="reportTab"
                    label="ICit"
                    insertBeforeQ="TabFormat"
                    >
                <group 
                        id="reportGroup" 
                        label="DBRW Copy Paste" 
                        autoScale="true"
                        >
                    <button 
                            id="DBRW_Copy"
                            label="DBRW Copy" 
                            imageMso="Copy"      
                            onAction="TM1Copy"
                            />
                    <button 
                            id="DBRW_Paste" 
                            label="DBRW Paste" 
                            imageMso="Paste"      
                            onAction="TM1Paste"
                            />
                    <button 
                            id="DBRW_UnDo" 
                            label="DBRW UnDo" 
                            imageMso="CellsDelete"      
                            onAction="TM1UndoPaste"
                            />
                    <toggleButton 
                            id="AutoCalc" 
                            label="AutoCalc" 
                            screentip="Toggle Autocalc"
                            size="large"
                            imageMso="ColumnSettingsMenu"      
                            onAction="OnAction"
                            getPressed="GetPressed"
                            />
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

在您的VBA中,首先创建一个全局变量,例如&#34; IsPressed&#34;。

然后&#34; GetPressed&#34;程序应该使用&#34; returnedVal&#34;变量返回值。

    Option Explicit

    Public MyRibbon As IRibbonUI
    Public IsPressed As Boolean

    Sub ToggleAutoCalc(ribbon As IRibbonUI)

        Set MyRibbon = ribbon

    End Sub

    Sub GetPressed(control As IRibbonControl, ByRef returnedVal)

        Select Case control.ID
          Case "AutoCalc"
            returnedVal = IsPressed
        End Select

    End Sub

    Sub OnAction(control As IRibbonControl, pressed As Boolean)

        Select Case control.ID
          Case "AutoCalc"
            IsPressed = pressed
            If IsPressed Then
              MsgBox IsPressed
            Else
              MsgBox IsPressed
            End If

        End Select

    End Sub

enter image description here