我按照directions here为Access应用程序创建了自定义功能区。但没有一个按钮工作!我一直收到一条错误,指出Access无法找到函数或宏,即使它是公共的并且在标准模块中。
最终我发现如果我使用以下语法它会起作用:
onAction="=fncMyFunction('string argument', 1234)"
fncMyFunction
接收手动输入的参数,但不接收功能区对象。
在另一个项目的Word中,我创建了一个自定义功能区,方法是将文档打开为.ZIP文件,在适当的位置添加XML,并添加对它的引用。 Relevant directions somewhere in this novel here
在Word中,我能够按照我预期的方式使用以下语法:
onAction="fncMyFunction"
在Word中,fncMyFunction
在单击按钮时传递了一个功能区对象。
这是什么交易?为什么语法不同?并且这种方式或其他方式“错误吗?”
答案 0 :(得分:10)
您应该使用功能区元素的tag
属性来存储您想要传递给操作的一些值。
例如,假设您有一个包含几个按钮的简单功能区:
ribbonOpenForm
,可在点击时打开表单FormDashBoardFinance
。ribbonDoAction
VBA 功能(不是Sub!)的通用操作LogOff("bye")
,例如,向用户显示消息,注销。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's Access 2010 Ribbon site