VBA通过.onAction传递参数 [英] VBA Pass arguments with .onAction

查看:905
本文介绍了VBA通过.onAction传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的潜水员的样子:

this is how my sub looks like:

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计算一个表达式.因此,根据我的经验,该表达式需要一个等号,并且就我而言,它必须是一个函数.只有自动本地回调才是subs.

编辑

我上面的答案以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传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆