calling a fuction and passing arguments with .onaction

blinkous

New Member
Joined
Jun 17, 2003
Messages
6
Duuudes:
Have a question for the programmers. I have created a right-click popup menu with vba. On one of the "msoControlButtons" I want the on action procedure to call a function that requires arguments to be passed to it. The onaction procedure requires a string so this confuses me. Let me show an ex:

here's my menu button:

Set MenuItem = InventorPopUp.Controls.Add(msoControlButton)
With MenuItem
.Caption = "mm: (all)"
.OnAction = "Function1"
End With

here's my function:

Public Function Function1(arg1 As String, arg2 As String)

do some stuff

End Function

Now as you can see for the onaction I told it to run "function1" which works great if there are no arguments to pass, but I don't understand how to call the function and pass the arguments. I have tried several formats such as

.onaction = "Function1(string1, string2)"
.onaction = "Function1(""string1"", ""string2"")"
.onaction = "Function1 "string1", "string2""
.onaction = Function1 "string1", "string2"

nothing seems to work. VbA keeps giving me an "Argument not Optional" error. I know i could create as many functions as I need to to cover every possible argument but it would be much easier to pass the arguments. Any help would be greatly appreciated.
 
Thanks ITGabs, you really helped me out! Only your code works in 2007 (when you can't use control parameters)!
I did some more testing and it also works in Excel 2010 and Excel 2013.
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I have an example with string arguments:

Code:
   .OnAction =  "'" & ThisWorkbook.Name & "'!'Importer_Items_Temp_par_Menu_Déroulant " & Chr(34) & Nom_Fichier & Chr(34) & " , " & Chr(34) & Old_Val & Chr(34) & " , " & Chr(34) & ThisWorkbook.Sheets("Import_Objets").Cells(Item_Num, q * 2).Value & Chr(34) & "'"


Where Importer_Items_Temp_par_Menu_Déroulant is a macro,
Nom_Fichier is a string variable,
Old_Val too,
ThisWorkbook.Sheets("Import_Objets").Cells(Item_Num, q * 2).Value is a string too, from a cell in a sheet.
 
Last edited:
Upvote 0
Thanks for this post!

a simple button
this is my code and work great in 2007
Code:
   Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
   With btn

     .OnAction = "'someProcedure ""Param1"", ""Param2"" '"
   End With

notice the pattern (adding spaces to make it clear)
OnAction = " ' someProcedure " " Param1 " " , " " Param2 " " ' "

I hope this help, I did a bit of brute force trying to make the onAction pass the params ;)

This is method will corrupt a xlsb file.
 
Upvote 0
You can pass argumentes with the .Parameter property, e.g.:

Code:
                With .Controls.Add(Type:=msoControlButton)
                    .Caption = "Caption"
                    .faceId = 177
                    .OnAction = "'" & ThisWorkbook.Name & "'!" & "someProcedure"
                    .Parameter = "hello"
                End With

Old post for sure.
Regards


May I know how to use this method if I have more than one parameters?
Thanks.
 
Upvote 0
I have been tearing my hair out.. after an hour or so of googling i could get no further, all i needed to get it to work were those two apostrophies!!
You sir are a genious!

I just did not see this ain anyone else's example. Mine would work without the parameteres, but not with, and I could not work out why.. Cheers!
:)

.on action = "'{function/sub} ""param"" '"

Thanks for this post!

a simple button
this is my code and work great in 2007
Code:
   Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
   With btn

     .OnAction = "'someProcedure ""Param1"", ""Param2"" '"
   End With

notice the pattern (adding spaces to make it clear)
OnAction = " ' someProcedure " " Param1 " " , " " Param2 " " ' "

I hope this help, I did a bit of brute force trying to make the onAction pass the params ;)
 
Upvote 0
Duuudes:
Have a question for the programmers. I have created a right-click popup menu with vba. On one of the "msoControlButtons" I want the on action procedure to call a function that requires arguments to be passed to it. The onaction procedure requires a string so this confuses me. Let me show an ex:

here's my menu button:

Set MenuItem = InventorPopUp.Controls.Add(msoControlButton)
With MenuItem
.Caption = "mm: (all)"
.OnAction = "Function1"
End With

here's my function:

Public Function Function1(arg1 As String, arg2 As String)

do some stuff

End Function

Now as you can see for the onaction I told it to run "function1" which works great if there are no arguments to pass, but I don't understand how to call the function and pass the arguments. I have tried several formats such as

.onaction = "Function1(string1, string2)"
.onaction = "Function1(""string1"", ""string2"")"
.onaction = "Function1 "string1", "string2""
.onaction = Function1 "string1", "string2"

nothing seems to work. VbA keeps giving me an "Argument not Optional" error. I know i could create as many functions as I need to to cover every possible argument but it would be much easier to pass the arguments. Any help would be greatly appreciated.
I know this post is old but perhaps this will help someone else.
You can use a "=functionCall(parameter)" in the OnAction property
Below is an example.

VBA Code:
Public Sub CreateShortcutMenu()
  On Error GoTo errHandler
  CommandBars("cmbPopUpMenu").Delete 'If commandbar exists, delete it; errHandler handles error when it doesn't exist
  Dim cmb As CommandBar, i As Long
  Set cmb = CommandBars.Add("cmbPopupMenu", msoBarPopup, False, False)
    With cmb
      i = 1
      .Controls.Add Type:=msoControlButton
      .Controls(i).Caption = "Button 1"
      .Controls(i).FaceId = 359 'Up Arrow face
      .Controls(i).OnAction = "=fxProcessOption('Button_1')"
      i = i + 1
      .Controls.Add Type:=msoControlButton
      .Controls(i).Caption = "Button 2"
      .Controls(i).FaceId = 360 'Down Arrow face
      .Controls(i).OnAction = "=fxProcessOption('Button_2')"
    End With
  Set cmb = Nothing
ExitSub:
  Exit Sub
errHandler:
  If Err.Number = 5 Then Resume Next 'cannot delete CommandBar doesn't exist, resume next statement
  MsgBox "Error in " & Application.VBE.ActiveCodePane.codemodule & ".CreateShortcutMenu: " & Err.Number & " - " & Err.Description
  Resume ExitSub
End Sub 'CreateShortcutMenu

Public Function fxProcessOption(pAction As String) As Boolean
  On Error GoTo errHandler
    fxProcessOption = False
  Select Case pAction
    Case Is = "Button_1"
      MsgBox "Add Code to Process Button 1"
    Case Is = "Button_2"
      MsgBox "Add Code to Process Button 2"
  End Select
ExitSub:
    fxProcessOption = True
  Exit Function
errHandler:
  MsgBox "Error in " & Application.VBE.ActiveCodePane.codemodule & ".fxProcessOption: " & Err.Number & " - " & Err.Description
  Resume ExitSub
End Function 'fxProcessOption
 
Upvote 0

Forum statistics

Threads
1,216,104
Messages
6,128,856
Members
449,472
Latest member
ebc9

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top