问题描述
这是我的潜艇的样子:
Sub InsertRowWithContent(rowNo As Long)
这是我的 .onAction:
This is my .onAction:
.OnAction = "'InsertRowWithContent""" & C & """'"
C 是之前声明的 Long 变量.
C is a Long variable declared earlier.
它说找不到宏.在添加参数之前它工作正常!
It says macro not found. It worked fine before adding an argument!
推荐答案
我已经成功地使用以下语法传递了参数:
I have sucessfully passed arguments with this syntax:
.OnAction = "=InsertRowWithContent(" & C & ")"
注意事项:
- C 很长.所以不要添加配额,就像在代码中调用 Sub 一样.
- OnAction 计算表达式.所以根据我的经验,表达式需要一个等号,就我现在而言,它必须是一个函数.只有自动本地回调是订阅者.
编辑
我上面的回答以 Access 为背景.Djikay 的答案适用于 Excel.然而,经过一番挖掘,我确信 Word 无法理解这两种语法中的任何一种.Word VBA 无法将参数传递给 OnAction
语句中的子项.至少目前最好接受这一点.
My answer above has Access as background. Djikay's answer works fine with Excel. Yet after some digging, I am quiet sure that simply Word doesn't understand either of those syntaxes. Word VBA cannot pass a parameter to a sub in an OnAction
statement. At least for the moment it's best to accept this.
但以下是使用 Word (2010) 明确运行的内容:
But here is what definitively runs with Word (2010):
创建您的命令栏和按钮.对于OnAction
,只传递Sub
的名称.但是使用按钮的 Parameter
属性.
Create your command bar and the button. For the OnAction
, only pass the name of the Sub
. But use the button's Parameter
property.
' Add bar
Set cdb = Application.CommandBars.Add("SomeTest", , False, False)
cdb.Visible = True
' Add button
Set cbb = cdb.Controls.Add
cbb.Caption = "PressMe"
cbb.OnAction = "TheCallback"
cbb.Parameter = 456
然后,通过CommandBars.ActionControl.Parameter
表达式访问参数:
Then, access the parameter by the CommandBars.ActionControl.Parameter
expression:
Public Sub TheCallback()
MsgBox "The parameter passed is: " & CommandBars.ActionControl.Parameter
End Sub
ActionControl
与 Access 下的 ActiveControl
非常相似(如果不相同):它是最后点击的控件.
ActionControl
is very similar (if not the same) as ActiveControl
under Access: it is the control that was clicked last.
来源:http://www.experts-exchange.com/编程/语言/Visual_Basic/Q_24982922.html
*phuu* :-)
*phuu* :-)
这篇关于VBA 通过 .onAction 传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!