自定义功能区 onAction 语法问题

发布于 2024-11-08 05:04:49 字数 715 浏览 4 评论 0原文

我按照此处的说明创建自定义功能区对于 Access 应用程序。但所有按钮都不起作用!我不断收到一条错误,指出 Access 无法找到该函数或宏,即使它是公共的且位于标准模块中。

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

onAction="=fncMyFunction('string argument', 1234)"

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

在另一个项目的 Word 中,我通过将文档作为 .ZIP 文件打开、在适当的位置添加 XML 并添加对其的引用来创建自定义功能区。 小说中的相关说明位于此处

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

onAction="fncMyFunction"

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

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

I followed the directions here to create a custom ribbon for an Access application. But none of the buttons worked! I kept getting an error that stated Access couldn't find the function or macro, even though it was public and in a standard module.

Eventually I discovered that it would work if I used the following syntax:

onAction="=fncMyFunction('string argument', 1234)"

fncMyFunction receives the manually typed in arguments, but not the ribbon object.

In Word for another project, I created a custom Ribbon by opening the document up as a .ZIP file, adding the XML in the appropriate place, and adding a reference to it. Relevant directions somewhere in this novel here.

In Word, I was able to have everything work the way I expected it to with the following syntax:

onAction="fncMyFunction"

In Word, fncMyFunction has a ribbon object passed to it when the button is clicked.

What's the deal here? Why the different syntax? And is one way or the other "wrong?"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多孤肩上扛 2024-11-15 05:04:49

您应该使用功能区元素的 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

Avenius Gunter 的 Access 2010 Ribbon 站点 是 Access Ribbon 的优秀资源。

You should use the ribbon element's tag property to store some values you want to pass to your action.

For instance, say you have a simple ribbon containing a few buttons:

  • the first button uses a generic action ribbonOpenForm that opens a form FormDashBoardFinance when clicked.
  • the second button uses a generic action ribbonDoAction that execute the LogOff("bye") VBA function (not a Sub!) that, for instance, displays a message to the user and logs off.
  • the last one duplicates the behaviour that you wanted for your 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>

The VBA to manage the ribbon would be in a module:

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

An excellent resource for the Access Ribbon is Avenius Gunter's Access 2010 Ribbon site

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文