Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

ms word - VBA Pass arguments with .onAction

this is how my sub looks like:

Sub InsertRowWithContent(rowNo As Long)

This is my .onAction:

.OnAction = "'InsertRowWithContent""" & C & """'"

C is a Long variable declared earlier.

It says macro not found. It worked fine before adding an argument!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have sucessfully passed arguments with this syntax:

.OnAction = "=InsertRowWithContent(" & C & ")"

Considerations:

  • C is a long. So don't add quotas, just as you wouldn't if you would call the Sub in the code.
  • OnAction evaluates an expression. Therefore according to my experience, the expression needs an equal sign, and as far as I now, it must be a function. Only automated local callbacks are subs.

EDIT

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.

But here is what definitively runs with Word (2010):

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

Then, access the parameter by the CommandBars.ActionControl.Parameter expression:

Public Sub TheCallback()

  MsgBox "The parameter passed is: " & CommandBars.ActionControl.Parameter

End Sub

ActionControl is very similar (if not the same) as ActiveControl under Access: it is the control that was clicked last.

Source: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_24982922.html

*phuu* :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...