ms-access - 自定义功能区 onAction 语法问题

标签 ms-access vba ms-word ms-office ribbon

我关注了directions here为 Access 应用程序创建自定义功能区。但所有按钮都不起作用!我不断收到一条错误消息,指出 Access 无法找到该函数或宏,即使它是公共(public)的且位于标准模块中。

最终我发现如果我使用以下语法它就会起作用:

onAction="=fncMyFunction('字符串参数', 1234)"

fncMyFunction 接收手动输入的参数,但不接收功能区对象。

在另一个项目的 Word 中,我通过将文档作为 .ZIP 文件打开、在适当的位置添加 XML 并添加对其的引用来创建自定义功能区。 Relevant directions somewhere in this novel here .

在 Word 中,我能够使用以下语法让一切按照我预期的方式工作:

onAction="fncMyFunction"

在 Word 中,单击按钮时,fncMyFunction 会传递一个功能区对象。

这是怎么回事?为什么语法不同?其中一种方式是“错误的”吗?

最佳答案

您应该使用功能区元素的 tag 属性来存储一些要传递给操作的值。

例如,假设您有一个包含几个按钮的简单功能区:

  • 第一个按钮使用通用操作 ribbonOpenForm,单击时会打开表单 FormDashBoardFinance
  • 第二个按钮使用通用操作 ribbonDoAction 来执行 LogOff("bye") VBA 函数(不是 Sub!)例如,向用户显示一条消息并注销。
  • 最后一个重复了您想要的 fncMyFunction() 行为。
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" 
          onLoad="ribbonLoad" loadImage="ribbonLoadImage">
   <ribbon startFromScratch="false">
        <tabs>
         <tab id="Home" label="Home">
                   <group id="gpDash" label="Dashboards">
                        <button id="btHomeFinance"
                                label="Finance"
                                imageMso="BlogHomePage"
                                onAction="ribbonOpenForm" 
                                tag="FormDashBoardFinance"/>
                        <button id="btLogOff"
                                label="Log Off"
                                imageMso="DatabasePermissionsMenu"
                                onAction="ribbonDoAction" 
                                tag="LogOff('bye')"/>
                        <button id="btMyFunc"
                                label="My Function"
                                imageMso="AppointmentColorDialog"
                                onAction="fncMyFunction" 
                                tag="'a string argument', 1234"/>
                   </group>
             </tab>
        </tabs>
   </ribbon>
</customUI>

管理功能区的 VBA 将位于模块中:

Option Compare Database
Option Explicit

' We keep a reference to the loaded Ribbon
Private ribbon As IRibbonUI

'-----------------------------------------------------------------------------
' Save a reference to the Ribbon
' This is called from the ribbon's OnLoad event
'-----------------------------------------------------------------------------
Public Sub ribbonLoad(rb As IRibbonUI)
    Set ribbon = rb
End Sub

'-----------------------------------------------------------------------------
' Open the Form specified by the ribbon control's Tag.
'-----------------------------------------------------------------------------
Public Sub ribbonOpenForm(control As IRibbonControl)
    DoCmd.OpenForm control.tag, acNormal
End Sub

'-----------------------------------------------------------------------------
' Perform the action specified by the ribbon control's Tag
' Use single quotes to delimit strings, they will be expanded.
' The action to be performed must be defined as a public Function!
'-----------------------------------------------------------------------------
Public Sub ribbonDoAction(control As IRibbonControl)
    Dim action As String
    action = Replace(control.Tag,"'","""")
    Eval action
End Sub

'-----------------------------------------------------------------------------
' fncMyFunction example implementation
' Use single quotes to delimit strings, they will be expanded.
'-----------------------------------------------------------------------------
Public Sub fncMyFunction(control As IRibbonControl)
    ' Split the string to separate the paramaters in the Tag
    Dim params As Variant
    params = Split(control.Tag, ",")
    ' Now we can assign each parameter
    Dim myString As String
    Dim myInt As Integer
    myString = Replace(Trim(params(0)),"'","") ' remove single quotes
    myInt = CInt(Trim$(params(1)))             ' We're expecting an Integer
    ' ... do something with the params ...
    Debug.Print myString  ' Will print: a string argument
    Debug.Print myInt * 2 ' Will print: 2468
End Sub

Access 功能区的一个优秀资源是 Avenius Gunter's Access 2010 Ribbon site

关于ms-access - 自定义功能区 onAction 语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6026895/

相关文章:

ms-access - "Operation not supported in transactions"在 Access 中复制/粘贴记录时

sql - 如何通过从当前日期时间减去日期时间字段来计算天数?

database - 大型数据库文件 (mdb) 在 vb.net 中加载需要时间,因此需要替代方案

excel - 运行宏时使用 Excel

excel - 将word文档解析成excel文件

mysql - 通过 VBA 代码在 MS Access 中不区分大小写的过滤器

vba - 有没有一种简单的方法可以在 vba 中选择多个条件?

excel - VBA - 反转功能

c# - Word 2013 分屏问题

c# - 使用格式信息读取 word 文件内容(图像和文本)